node.js - nodejs引入webpack打包后的项目里的index.html,把html格式识别成js??
ringa_lee
ringa_lee 2017-04-17 16:00:58
[Node.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_lee
ringa_lee

ringa_lee

全部回复(1)
阿神

第一次请求,index.html
第二次请求,favicon图标
第三次请求,bundle.js文件

但是你写的路由只要有访问就返回index.html所以,第三次访问时,想要一个js文件,就把你返回的html文件当js啦

建议使用static中间件来解决静态文件访问,或者分别写路由

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号