Ускорение и облачные функции - PullRequest
0 голосов
/ 13 декабря 2018

я пытаюсь развернуть пример в облачных функциях для тестирования и не работает, мой код:

`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 as express

1 Ответ

0 голосов
/ 11 февраля 2019

эта проблема была объяснена в 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)
  })
})
...