我开发了一个在线石头剪刀布游戏,并决定部署它。在我的机器上一切正常。我决定在Heroku上部署我的服务器,在我这样做之后,我得到“Cannot GET /”和“to load resource:服务器在控制台中以状态404(未找到)响应。
作为参考,这是我在服务器文件中的index.js:
const express = require("express");
const app = express();
const http = require("http");
const { Server } = require("socket.io");
const socketio = require("socket.io");
const cors = require("cors");
app.use(cors());
const server = http.createServer(app);
let players = [];
let otherPlayerChoice = null;
let playerChoice = null;
// const io = new Server(server, {
// cors: {
// origin: "https://rock-paper-scissors-online.herokuapp.com",
// methods: ["GET", "POST"],
// },
// });
const io = socketio(server);
io.on("connection", (socket) => {
console.log(`User Connected: ${socket.id}`);
players.push(socket.id);
socket.on("join-room", (number) => {
socket.join(number);
});
const playerNumber = players.indexOf(socket.id);
socket.emit("playerNumber", playerNumber);
socket.on("disconnect", () => {
console.log(`User Disconnected: ${socket.id}`);
const index = players.indexOf(socket.id);
players.splice(index, 1);
});
socket.on("choice", (data) => {
console.log(`Player ${data.playerNumber} chose ${data.choice}`);
// Find the other player
const otherPlayer = players.find((player) => player !== socket.id);
// Get the other player's choice
socket.to(otherPlayer).emit("opposing-player-move", data.choice);
socket.on("test", (move) => {
otherPlayerChoice = move.choice;
playerChoice = data.choice;
// Determine the winner
let result;
if (playerChoice === otherPlayerChoice) {
result = "It's a tie!";
} else if (
(playerChoice === 0 && otherPlayerChoice === 2) ||
(playerChoice === 1 && otherPlayerChoice === 0) ||
(playerChoice === 2 && otherPlayerChoice === 1)
) {
result = `Player ${data.playerNumber === 0 ? 1 : 0} wins!`;
} else {
result = `Player 2 wins!`;
}
// Emit the result to both players
io.to(move.room).emit("game-result", result);
// io.to(otherPlayer).emit("game-result", result);
io.emit("disable-buttons");
});
});
});
server.listen(process.env.PORT || 5000, () => {
console.log("SERVER IS RUNNING");
});
这是我的Heroku日志:
2023-04-12T17:05:08.265241+00:00 app[web.1]: SERVER IS RUNNING
2023-04-12T17:05:08.543766+00:00 heroku[web.1]: State changed from starting to up
2023-04-12T17:05:10.135071+00:00 heroku[router]: at=info method=GET path="/" host=rock-paper-scissors-online.herokuapp.com request_id=1ee5c19c-7db9-48bf-a0da-dc83b386c9b2 fwd="109.96.9.152" dyno=web.1 connect=0ms service=5ms status=404 bytes=415 protocol=https
我的代码有什么问题?
1条答案
按热度按时间aemubtdh1#
找到问题了。我没有根路由,所以我创建了一个。