简体   繁体   中英

Pushing nodejs backend to heroku error with tail logs

2021-12-07T05:04:44.677016+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:950:32)
2021-12-07T05:04:44.677017+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:790:12)
2021-12-07T05:04:44.677017+00:00 app[web.1]:     at Module.require (internal/modules/cjs/loader.js:974:19) {
2021-12-07T05:04:44.677017+00:00 app[web.1]:   code: 'MODULE_NOT_FOUND',
2021-12-07T05:04:44.677018+00:00 app[web.1]:   requireStack: [ '/app/Routers/auth.js', '/app/index.js' ]
2021-12-07T05:04:44.677018+00:00 app[web.1]: }
2021-12-07T05:04:44.831688+00:00 heroku[web.1]: Process exited with status 1
2021-12-07T05:04:44.901482+00:00 heroku[web.1]: State changed from starting to crashed
2021-12-07T05:05:00.631822+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=atlas-backnd.herokuapp.com request_id=715326ba-27df-4dbc-a6b0-9d4bfcf53418 fwd="208.107.191.229" dyno= connect= service= status=503 bytes= protocol=https
2021-12-07T05:05:01.147207+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=atlas-backnd.herokuapp.com request_id=d76168fe-64ac-4141-8895-e6e72631f534 fwd="208.107.191.229" dyno= connect= service= status=503 bytes= protocol=https

// server
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const helmet = require("helmet");
const morgan = require("morgan");
const multer = require("multer");
const path = require("path");
const cors = require('cors')
const authRoute = require("./Routers/auth");
const postsRoute = require("./Routers/posts");
const usersRoute = require("./Routers/users");
const convoRoute = require("./Routers/Conversation");
const messageRoute = require("./Routers/Message");
const app = express();

dotenv.config();

mongoose.connect(
  process.env.MONGO_URL,
  { useNewUrlParser: true, useUnifiedTopology: true },
  () => {
    console.log("Connected to MongoDB");
  }
);
app.use("/images", express.static(path.join(__dirname, "public/images")));

app.get('/', (req, res) => {
  res.send('Home Page.')
})


//middleware
app.use(express.json());
app.use(cors())
app.use(helmet());
app.use(morgan("common"));

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "public/images");
  },
  filename: (req, file, cb) => {
    cb(null, req.body.name);
  },
});

const upload = multer({ storage: storage });
app.post("/api/upload", upload.single("file"), (req, res) => {
  try {
    return res.status(200).json("File uploded successfully");
  } catch (error) {
    console.error(error);
  }
});

app.use("/api/auth", authRoute);
app.use("/api/users", usersRoute);
app.use("/api/posts", postsRoute);
app.use("/api/conversations", convoRoute);
app.use("/api/messages/", messageRoute);

app.listen(5000, () => {
  console.log("Backend server is running!");
});

const io = require("socket.io")(8000, {
  cors: {
    origin: "http://localhost:3000",
  },
});

let users = [];

const addUser = (userId, socketId) => {
  !users.some((user) => user.userId === userId) &&
    users.push({ userId, socketId });
};

const removeUser = (socketId) => {
  users = users.filter((user) => user.socketId !== socketId);
};

const getUser = (userId) => {
  return users.find((user) => user.userId === userId);
};

io.on("connection", (socket) => {
  //when ceonnect

  //take userId and socketId from user
  socket.on("addUser", (userId) => {
    addUser(userId, socket.id);
    io.emit("getUsers", users);
  });

  //send and get message
  socket.on("sendMessage", ({ senderId, receiverId, text }) => {
    const user = getUser(receiverId);
    io.to(user.socketId).emit("getMessage", {
      senderId,
      text,
    });
  });

  //when disconnect
  socket.on("disconnect", () => {
    removeUser(socket.id);
    io.emit("getUsers", users);
  });
});

I want to push my backend to Heroku. The issue seems to be a module not being found. The application worked normally when I started it on my local machine but it breaks. I've also added a profile to give heroku more information. I'm not sure how to fix it. Tried to look at the documentation but got lost. I provided the tail logs Heroku sent me. Also provided my server code. I would appreciate any help I can get, Thanks.

looking at the code, port is hard coded ie, 5000 and 8000 (socket.io).

You have refactor the code. The port should be dynamic.


const express = require("express");
const path = require("path");
const http = require("http");
const socketio = require("socket.io");


const app = express();
const server = http.createServer(app);
const io = socketio(server);

const PORT = process.env.PORT; // OR const PORT = process.env.PORT || 3000;
const publicDirectoryPath = path.join(__dirname, "../public");

app.use(express.static(publicDirectoryPath));

io.on("connection", (socket) => {
  console.log("New WebSocket connection");

});

server.listen(PORT, () => {
  console.log("server is up at ", PORT);
});


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM