当我尝试使用非常简单的应用程序运行本地节点服务器时,我收到以下错误消息(请参阅下面的编码)。
由于不允许的 MIME 类型(“text/html”),从“ http://localhost:8080/importing.js ”加载模块被阻止。
我是 node 和 ES6 模块的新手,所以我不太了解问题的细节。根据此URL,必须为模块显式提供 MIME 类型“应用程序/javascript”。但是我如何在下面的示例中实现这一点?
索引.html
<!DOCTYPE html>
<html>
<head>
<script src="./importing.js" type="module"></script>
<meta charset="utf-8">
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
服务器.js
var http = require('http');
var fs = require('fs');
const PORT=8080;
fs.readFile('./index.html', function (err, html) {
if (err) throw err;
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(PORT);
});
Run Code Online (Sandbox Code Playgroud)
导入.js
import {a} from './exporting.js';
console.log(a);
Run Code Online (Sandbox Code Playgroud)
导出.js
export const a = 'Constant a';
Run Code Online (Sandbox Code Playgroud)
我用 CMD 启动服务器
node server.js
Run Code Online (Sandbox Code Playgroud) 我有一个打字稿项目工作正常.我创建了第二个项目,并通过文件系统复制了我以前的一些代码.除了我的.js现在与我的.ts不同步之外,一切正常.我现在如何解决问题:删除两个文件并通过拖动ts文件(将自动携带.js)再次使用Visual Studio界面复制 - 无论如何,我很好奇,现在我想知道,.ts是怎么回事并且.js互相认识?在ASP.NET中你有背后的代码概念.但是在这里我看不出这是怎么做的.Visual Studio是否将关系保存在隐藏文件中?