【问题标题】:Nodejs fastify crash heruku apiNode Js fastify crash heroku api
【发布时间】:2021-05-05 21:19:37
【问题描述】:

我正在尝试加载一个用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

【问题讨论】:

    标签: javascript node.js heroku routes fastify


    【解决方案1】:

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

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

    【讨论】:

    • 好吧,它似乎工作。他们在文档中找不到任何关于此的内容。代码的第二部分究竟做了什么?
    • 我已将链接添加到文档以获取详细信息。第二部分,记录器,用于检查您的服务器发生了什么
    • 如果使用 fastify-cli,设置 env var FASTIFY_ADDRESS=0.0.0.0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2021-12-03
    • 2014-05-23
    • 2014-07-04
    • 2017-10-18
    • 2016-07-25
    相关资源
    最近更新 更多