带有 es6 模块的基本应用程序无法正常工作 chrome - ERR_ABORTED 404(未找到)

basic app with es6 modules not working chrome - ERR_ABORTED 404 (Not Found)

我正在使用 es6 模块创建一个基本应用程序并与节点服务器进行交互。 index.html 将 index.js 文件作为模块提供服务,但该模块未加载且控制台显示错误: GET http://localhost:3000/index.js net::ERR_ABORTED 404(未找到)

https://codesandbox.io/embed/veriz-210fb

https://github.com/stanleyjohnson/veriz

要重现:clone repo, npm i, npm start, go to http://localhost:3000

您应该通过将以下行添加到 server.js 文件来启用 static files serving

app.use('/', express.static(__dirname + '/'));

因此您的文件应如下所示:

const express = require('express')
const app = express()
const port = 3000

app.use('/', express.static(__dirname + '/'));

app.get('/', function(req, res) {
    res.sendFile('index.html',{ root: __dirname });
});

app.listen(port);