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次???
第一次要求,index.html
第二次請求,favicon圖示
第三次請求,bundle.js檔案
但是你寫的路由只要有訪問就返回index.html所以,第三次訪問時,想要一個js文件,就把你返回的html文件當js啦
建議使用static中間件來解決靜態檔案訪問,或分別寫入路由