0
所以我有一些代码(app.js,服务器)的Node.js/express出错:无法获取/
console.log("Server started. If you're reading this then your computer is still alive."); //Unnecessary test command to make sure everything works.
var express = require("express");
var app = express();
var serv = require("http").Server(app);
app.get("/", function(req, res) {
res.sendFile(__dirname + "/client");
});
app.use("/client", express.static(__dirname + "/client"));
serv.listen(2000);
//Set up server stuff. This isn't touched.
var io = require("socket.io")(serv, {});
io.sockets.on("connection", function(socket) {
console.log("Socket connection"); //This will print to the server, not the developer console in your browser.
});
//Initialize sockets and set up socket listeners. This isn't touched either, except when adding new events.
和一些更多的代码:
<!DOCTYPE html>
<html>
<head>
<title>Multiplayer!</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<script>
var socket = io();
</script>
</body>
</html>
我的文件夹结构:
multiplayer_game
app.js
package.json
node_modules (folder)
node stuff
client (folder)
index.html
js (folder)
img (folder)
server (folder)
如果文件越远,它就越“嵌套”。
当我通过做node app.js
(已经cd'ed的文件夹)打开页面并转到localhost:2000
时,我收到“Can not GET /”。当我去localhost:2000/client
,我得到的一切都很好。我能做些什么来修复我的代码?
谢谢,这工作!如果可以的话,你能解释一下这个“路由器”是什么吗? –
@ Someone'sAlt路由器只是一个简单的中间件与快递,知道如何处理路由和http消息。 – Burimi
当我运行它时,'app.use(“/”,router)'部分使得我的服务器在通过localhost:2000加载时滞后。我在此发布了一个问题 –