0

I am hosting my very simple nodejs server in Heroku. But, when I try it, it returns this error:

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`

Here's the server.js:

const express = require("express");
const cors = require("cors");

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

const server = express();

server.use(cors());

server.get("/", (req, res) => {
  const INDEX = "/index.html";
  res.sendFile(INDEX, { root: __dirname });
});

server.get("/test", (req, res) => {
  res.send("test Page");
});

server.listen(PORT, () => console.log(`listening on port ${PORT}`));

package.json:

{
"name": "express-heroku",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "engines": {
    "node": "15.11.x"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "mongoose": "^5.11.19"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

Don't know what the reason is, but, when I try this in the localhost it works perfectly!

The full error on Heroku CLI: enter image description here

Any help is greatly appreciated !

Ajit Kumar
  • 367
  • 1
  • 10
  • 35
  • 2
    Like it says, check the logs for more details. It'll be hard to debug without an error message. – CertainPerformance Mar 10 '21 at 15:46
  • Here's what I got from the Heroku console -> `2021-03-10T15:48:28.602246+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=creativity-server.herokuapp.com request_id=bb9535df-ae21-4198-88bc-8ba20c5d092d fwd="157.44.142.5" dyno= connect= service= status=503 bytes= protocol=https 2021-03-10T15:48:29.303442+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=creativity-server.herokuapp.com request_id=5f7a1b87-0fb7-42a0-a04a-3aab5d7b94a4 fwd="157.44.142.5" dyno= connect= service= status=503 bytes= protocol=https` – Ajit Kumar Mar 10 '21 at 15:48
  • Hers's the image -> https://i.stack.imgur.com/bgxJk.png – Ajit Kumar Mar 10 '21 at 15:50
  • did you set up correctly the environment variables? – Cmte Cardeal Mar 10 '21 at 15:54
  • Did you mean the `process.env.PORT` ? I thought that it was already defined by Heroku itself ! – Ajit Kumar Mar 10 '21 at 15:57
  • yes, but you have to declare `PORT` with no value on _config vars_ page. – Cmte Cardeal Mar 10 '21 at 16:18
  • 1
    I found that there is no need for declaring `PORT` https://stackoverflow.com/a/52806264/14942924 – Ajit Kumar Mar 10 '21 at 16:26
  • Could you find the updated code in the question – Ajit Kumar Mar 10 '21 at 16:30
  • Please post you package.json – Zam Abdul Vahid Mar 10 '21 at 16:37
  • Are you able to scroll up in the error log? The original reason/error might be logged earlier/above the parts you've shared. – chillin Mar 10 '21 at 17:01
  • Oh God ! Why I didn't notice that before ? The error was in the `package.json` ! There was no mention of `cors` in the dependencies list. That's why the app crashed ! Thanks everyone ! – Ajit Kumar Mar 11 '21 at 09:23

1 Answers1

0

Create a ProcFile: enter image description here

Inside the ProcFile add web: node index.jsenter image description here

Doing this, you are telling heroku to run your server, with node.

Flash
  • 169
  • 1
  • 6