我在用express
使用 es6 模块提供带有 js 文件的页面。
关于 es6 模块 -https://jakearchibald.com/2017/es-modules-in-browsers/ https://jakearchibald.com/2017/es-modules-in-browsers/
我的 server.js 文件是 -
const app = express();
app.use( express.static( __dirname + '/src' ));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(8080, () => console.log('Listening on port 8080!'));
我的index.html是-
<html lang="en">
<body>
<script type="module" src="./src/test.js"></script>
</body>
</html>
在文件中test.js
我正在使用 es6 模块,因此type="module"
在脚本标签中。
But test.js
当我在浏览器中提供此 html 时,脚本未加载。它给出了错误 -
Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
我尝试在express中使用它添加js文件的mime类型server.js
文件 -
express.static.mime.define({'application/javascript': ['js']});
但结果还是一样。我想我需要以某种方式从 js 文件的 Express 发送 mime/content-type,但是如何呢?
请帮忙。
它需要一个安装路径。'/src'
in -
app.use( '/src', express.static( __dirname + '/src' ) );
但不知道为什么。将会更新。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)