Search code examples
javascriptherokudeploymentserversocket.io

Heroku: failed to load resources


I developed a online rock-paper-scissors game and decided to deploy it. Everything works fine on my machine. I decided to deploy my server on Heroku and after i did, i get "Cannot GET /" and "to load resource: the server responded with a status of 404 (Not Found)" in the console.

For reference this is my index.js in the server file:

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");
});

this is my heroku logs:

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

What is wrong with my code ?


Solution

  • Found the problem. I didn't have a root route so I created one.

    const express = require("express");
    const router = express.Router();
    
    router.get("/", (req, res) => {
      res.send("Server is up!");
    });
    
    module.exports = router;