尝试提供静态 JS 文件 Node.js,错误 404

Trying to serve static JS files Node.js, Err 404

我正在尝试设置一个简单的 node.js 服务器来执行一些基本的 Socket.io 工作,但是当我尝试提供我的静态 JS 文件时,出现此错误:

获取 https://somewebsitewithfiles.website net::ERR_ABORTED 404

这是我的服务器和本地代码:

服务器:

var express = require('express');  
var app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;


app.get('/', (req, res) => {
  res.sendFile(__dirname + '/HTML/index.html');
});
app.use(express.static('Static'))

http.listen(port, () => {
  console.log(`Socket.IO server running at http://localhost:${port}/`);
})

本地:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script src="/Static/User.js"></script>
  
</body>
</html>

如您所见,我使用了快速静态文件命令 似乎不起作用。我的文件系统由我的项目文件夹组成,里面是我的服务器 JS 文件。那里还有一个名为“Static”的文件夹,其中包含我的静态文件和一个名为 HTML 的文件夹,其中包含我的 index.html

感谢任何帮助。谢谢

app.use(express.static('Static')) 尝试将此行放在你的“/”路线上方

代码:

var express = require('express');  
var app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;

app.use(express.static('Static'))
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/HTML/index.html');
});


http.listen(port, () => {
  console.log(`Socket.IO server running at http://localhost:${port}/`);
})

编辑:

<script src="/Static/User.js"></script>

您不必在 src 路径中提及 'static'。

正确方法:

<script src="/User.js"></script>