【发布时间】:2021-10-03 12:18:17
【问题描述】:
我正在尝试设置一个简单的 node.js 服务器来完成一些基本的 Socket.io 工作,但是当我尝试提供我的静态 JS 文件时,我收到了这个错误:
GET https://somewebsitewithfiles.websitenet::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>
如您所见,我使用了 express static file 命令,女巫似乎不起作用。我的文件系统由我的项目文件夹组成,里面是我的服务器 JS 文件。那里还有一个名为“Static”的文件夹,其中包含我的静态文件和一个名为 HTML 的文件夹,其中包含我的 index.html
任何帮助表示赞赏。谢谢
【问题讨论】:
标签: javascript node.js server socket.io