Search code examples
node.jsheroku

Simple Node.js Heroku deployment fails


I have this simple web-server named index.js:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/data', (req, res) => {
    res.send('Data')
  })

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

I added the Procfile:

web:node server.js

I added the engines in my package.js:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon ./index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.3",
    "nodemon": "^2.0.15"
  },
  "engines": {
    "node": "16.13.1",
    "npm": "8.1.2"
  }
}

My deployment keeps failing with my heroku logs --tail being the following:

2022-03-04T09:10:22.196622+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=sismographie-back.herokuapp.com request_id=13ad01a2-0ff4-469e-820e-d61a337014e0 fwd="92.184.123.97" dyno= connect= service= status=503 bytes= protocol=https
2022-03-04T09:10:22.764644+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=sismographie-back.herokuapp.com request_id=09b110c3-0b16-45c0-8c02-02c77a7907c0 fwd="92.184.123.97" dyno= connect= service= status=503 bytes= protocol=http

If this matters, I'm using the /Deploy > Deploy Branch built-in feature.

What is happening?

Thanks.


Solution

  • You cannot use 3000 as your default port on heroku.You should use heroku's port.The problem is basically arising due to the port.

    For working of it change your code like -

    app.listen(process.env.PORT || port, () => {
    console.log(`Example app listening on port ${port}`)
    });
    

    Now heroku will pick a random port based on their server and if they can't fetch any your port will have presendence over it.