【问题标题】:js file is not getting loaded while serving html from nodejs [duplicate]从nodejs提供html时,js文件没有被加载[重复]
【发布时间】:2018-09-29 02:12:35
【问题描述】:

我正在尝试从节点服务器提供 index.html 文件,但我收到此错误 "localhost/:1 Refused to execute the script from 'http://localhost:3001/assets/app.js' 因为它的 MIME 类型 ('text/html' ) 不可执行,并且启用了严格的 MIME 类型检查。”

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Common JS Example</title>
</head>
<body>
    <h1>CommonJS</h1>
    <script type="text/javascript" src="./assets/app.js"></script>
</body>
</html>

server.js

var express = require('express');
var path = require('path');
var app = express();


app.use(express.static(path.resolve(__dirname, "assets")));

app.get('/',function(req,res){
    res.sendFile(path.resolve(__dirname, "./index.html"))
})

app.listen(3001, () => console.log('Example app listening on port 3001!'))

app.js

console.log('hiiii')

【问题讨论】:

  • 对不起,我知道这是一个非常幼稚的问题,但我最近开始研究 nodeJS

标签: javascript node.js


【解决方案1】:

“assets”是静态路径,所以修改脚本src为

<script type="text/javascript" src="/app.js"></script>

它应该可以工作

解释here

【讨论】:

  • 是的,它奏效了。非常感谢。
  • 当然。上面的 ExpressJS 文档编辑解释了原因
猜你喜欢
  • 2015-10-15
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
相关资源
最近更新 更多