我如何解决此错误:localhost /:1无法加载资源:服务器以404(未找到)状态响应,无法获取/

问题描述 投票:0回答:1

[我正在创建类似于https://diep.io/https://agar.io/的在线多人.io游戏,并且我正在设置服务器,这是我的代码,

var path = require('path');
var http = require('http'); 
var express = require('express'); 
var socketIO = require('socket.io'); 

var publicPath = path.join(__dirname, '../client');
var port = process.env.PORT || 2000;
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
app.use(express.static(publicPath));

server.listen(port, function () {
    console.log('Server stared on port ' + port);

});

当我启动服务器并将“ localhost:2000”放入搜索栏中时,就会出现这种情况,

enter image description here

我对此很陌生,所以如果您能使答案简单易懂,那么我将不胜感激。

javascript html server linker-errors 2d-games
1个回答
0
投票

仔细检查您的路径。如果您具有这样的文件夹结构,您的代码将按编写的方式工作:

/server/index.js
/client/index.html

$ node server/index.js

默认情况下,静态中间件将提供与html,html匹配的文件。因此,如果您有index.html,则可以向/发出请求,如果您有hello.html,则可以在/ hello处命中它。

请参见此处以获取更多选项:http://expressjs.com/en/resources/middleware/serve-static.html

注意:Express.static使用的是此中间件,因此无需安装该软件包,只需查看serveStatic()的示例并将其传递给Express'版本。

© www.soinside.com 2019 - 2024. All rights reserved.