0

I'm trying to load a simple api written with nodejs and fastify, as seen in the code below.

When I try to open the page I get such an error in the logs, and the page gives me Application error.

Can you give me a hand?

at=error code=H10 desc="App crashed" method=GET path="/" ... dyno= connect= service= status=503 bytes= protocol=https

package.json

{
  "name": "fastPro",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "nodemon src/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fastify": "^3.11.0",
    "nodemon": "^2.0.7"
  }
}

index.js

const fastify = require('fastify')()

// Declare a route
fastify.get('/', (request, reply) => {
    reply.send({ hello: 'world!' })
})

// Run the server!
fastify.listen(process.env.PORT, (err, address) => {
    if (err) throw err
    fastify.log.info(`server listening on ${address}`)
})

Procfile

web: node src/index.js
worker: node src/index.js
Paul
  • 3,644
  • 9
  • 47
  • 113

1 Answers1

3

To let Fastify works on herouku you need to listen for all the incoming IPs:

fastify.listen(process.env.PORT, '0.0.0.0', (err, address) => {
    if (err) throw err
    fastify.log.info(`server listening on ${address}`)
})

I would suggest setting the logger to read some insight into your server

const fastify = require('fastify')({ logger: process.env.LOG_LEVEL || false })

to turn on/off more verbose logging when needed.

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • Ok, it seems to work. They find nothing in the documentation about this. What exactly does the second part of the code do? – Paul Feb 01 '21 at 17:51
  • I have added the links to the docs for details. The second part, the logger, is needed to check what is happening into your server – Manuel Spigolon Feb 02 '21 at 07:29
  • 2
    If using fastify-cli, set env var `FASTIFY_ADDRESS=0.0.0.0` – Marius Mar 16 '21 at 15:43