Nodejs fastify 崩溃 heruku api

Nodejs fastify crash heruku api

我正在尝试加载用 nodejsfastify 编写的简单 api,如下面的代码所示。

当我尝试打开页面时,我在 logs 中遇到这样的错误,并且页面给我 Application 错误。

你能帮帮我吗?

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}`)
})

过程文件

web: node src/index.js
worker: node src/index.js

要让 Fastify 在 herouku 上运行,您需要 listen 为所有传入 IP:

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

我建议设置 logger 以阅读对您的服务器的一些见解

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

在需要时 on/off 进行更详细的日志记录。