问题描述
我正在尝试加载一个用 nodejs
和 fastify
编写的简单 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 })
在需要时打开/关闭更详细的日志记录。