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加載時滯後。我在此發佈了一個問題 –