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次???
First request, index.html
Second request, favicon icon
Third request, bundle.js file
But the route you wrote will return index.html as long as there is access. So, when you visit for the third time, if you want a js file, treat the html file you returned as js
It is recommended to use static middleware to solve static file access, or write routes separately