node.js - nodejs引入webpack打包后的项目里的index.html,把html格式识别成js??
ringa_lee
ringa_lee 2017-04-17 16:00:58
0
1
682

webpack把js等文件打包到/dist/build.js里,我在index.html里引入这个js。直接这样点击index.html,是有效果出来的。说明打包过程没出错。

现在我用nodejs构建服务端,当我把这个index.html文件sendFile过去,报错,把index.html认成build.js??

错误信息

index.js代码

const express = require('express')

const app = express();

app.get('*',function(req, res) {
    res.sendFile('index.html',{root:__dirname+"/../"},function(err) {
        if(err) {
            console.log(err)
        }else {
            console.log('yes');
        }
    });
})

app.listen(3000,function() {
    console.log('example app listening at 3000');
})

而且,我localhost:3000/的时候,发现控制台输出了三个yes。。。为啥的???执行了3次???

ringa_lee
ringa_lee

ringa_lee

全員に返信(1)
阿神

最初のリクエスト、index.html
2 番目のリクエスト、ファビコン アイコン
3 番目のリクエスト、bundle.js ファイル

ただし、作成したルートはアクセスされる限り、index.html を返します。そのため、3 回目にアクセスするときに、js ファイルが必要な場合は、返された html ファイルを js として扱います。

静的ファイルアクセスを解決するには静的ミドルウェアを使用するか、ルートを個別に記述することをお勧めします

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート