1

我尝试在云功能中部署一个示例进行测试但不起作用,我的代码是:

`const functions = require('firebase-functions'); 
const Fastify = require('fastify') 
const fastify = Fastify() 
fastify.get("/",async (req, reply) =>{ 
reply.send({ hello: "world" }) 
}) 
fastify.listen(3000) 
module.exports = { api: functions.https.onRequest(fastify) };`

有人知道如何将fastify的服务器部署为express

4

1 回答 1

2

前几天 Fastify 已经解释了这个问题。

您可以在此处查看维护人员的完整说明

我将在这里发布工作解决方案:

const functions = require('firebase-functions')
const http = require('http')
const Fastify = require('fastify')

let handleRequest = null

const serverFactory = (handler, opts) => {
  handleRequest = handler
  return http.createServer()
}
const fastify = Fastify({serverFactory})

fastify.get('/', (req, reply) => {
  reply.send({ hello: 'world' })
})

exports.app = functions.https.onRequest((req, res) => {
  req = Object.assign({ip: ''}, {...req});
  fastify.ready((err) => {
    if (err) throw err
    handleRequest(req, res)
  })
})
于 2019-02-11T07:41:32.000 回答