Nodejs fastify 崩溃 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 上运行,您需要为所有传入的 IP 设置 listen

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

在需要时打开/关闭更详细的日志记录。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...