搜尋

首頁  >  問答  >  主體

node.js - nodejs引入webpack打包后的项目里的index.html,把html格式识别成js??

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_leeringa_lee2876 天前685

全部回覆(1)我來回復

  • 阿神

    阿神2017-04-17 16:02:58

    第一次要求,index.html
    第二次請求,favicon圖示
    第三次請求,bundle.js檔案

    但是你寫的路由只要有訪問就返回index.html所以,第三次訪問時,想要一個js文件,就把你返回的html文件當js啦

    建議使用static中間件來解決靜態檔案訪問,或分別寫入路由

    回覆
    0
  • 取消回覆