Home > Mobile >  Deployment of Nextjs and Expressjs on heroku keeps show error
Deployment of Nextjs and Expressjs on heroku keeps show error

Time:07-24

I want to deploy a project built with expressjs and nextjs to heroku. I want to buid a static file after deploying by using heroku-postbuild method in my package.json script. I've spent so many hours trying to make this work but I was unable to. I followed a tutorial where the teacher do this with react, but am not sure if same thing applied to nextjs. I also run the build script myself on the frontend and deploy it afterward, but I still get same result. The app was deployed successfully, but when I open it on heroku, it shows an error page with this content ``` Application error

An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command heroku logs --tail` Here is what my code looks like.

// ------------server.js----------------
const dbConnect = require("./src/lib/dbConnect.js");
const express = require("express");
const rootRoute = require("./src/root_Route");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const dotenv = require("dotenv");
const path = require("path");
dotenv.config();

dbConnect();
const app = express();

app.set("trust proxy", 1);

app.use(express.json());
app.use(cookieParser());
app.use(
  cors({
    origin: ["http://localhost:3000"],
    credentials: true,
    methods: ["GET", "POST", "PUT", "DELETE"],
  })
);

rootRoute(app);

// Serve static assets if in production
if (process.env.NODE_ENV === "production") {
  app.use(express.static("frontend/out"));

  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "frontend", "out", "index.html"));
  });
}

const PORT = process.env.PORT || 5000;

app.listen(PORT, (req, res) => console.log(`Server running on PORT:${5000}`));
//----------------package.json script-----------------

  "scripts": {
    "start": "node server",
    "push-to-live": "git push heroku master",
    "server": "nodemon server",
    "client": "cd ./frontend && yarn dev",
    "dev": "concurrently \"yarn server\" \"yarn client\"",
    "heroku-postbuild": "NPM__CONFIG_PRODUCTION=false cd ./frontend && yarn && yarn build "
  },

Here is the error I was getting on my heroku log: at=error code=H10 desc="App crashed" method=GET path="/" host=naira-only.herokuapp.com request_id=a92f9b80-ba50-4fa2-a1fa-9aa009f16232 fwd="102.89.32.125" dyno= connect= service= status=503 bytes= protocol=https

CodePudding user response:

Thanks to anyone who attempt to help me out. I didnot add my .env values on heroku. So it crash cause it can't read my mongoDB url value

  • Related