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 Routerは、ルートとhttpメッセージの処理方法を知っている、Expressに同梱されているシンプルなミドルウェアです。 – Burimi
'app.use("/"、router)'部分を実行すると、localhost:2000経由でロードするときにサーバが遅れることがあります。私はこれに質問を掲示した –