-
-
Save shwei/d34c7383a117451c07dc701ad12482f1 to your computer and use it in GitHub Desktop.
'use strict'; | |
const functions = require('firebase-functions'); | |
const fastify = require('./fastify'); | |
exports.greetFromFastify = functions.https.onRequest(fastify); |
'use strict'; | |
const http = require('http'); | |
const Fastify = require('fastify'); | |
let handleRequest = null; | |
const serverFactory = (handler, opts) => { | |
handleRequest = handler; | |
console.log('server factory opts: %j', opts); | |
return http.createServer(); | |
}; | |
const fastify = Fastify({ | |
serverFactory, | |
logger: true, | |
// skipHandleTrustProxy: true // proposed addition to the options | |
}); | |
fastify.get('/get', (req, reply) => { | |
reply.send('[Fastify] Response from GET request'); | |
}); | |
fastify.post('/post', (req, reply) => { | |
reply.send('[Fastify] Response from POST request'); | |
}); | |
module.exports = (req, res) => { | |
console.log('req.ip: %s', req.ip); // req.ip: 127.0.0.1 | |
/* | |
Without the next line, an error would occur | |
error: /Users/shwei/Work/workspace/cf-fastify-restify-express/functions/node_modules/fastify/fastify.js:249 | |
req.ip = req.connection.remoteAddress | |
^ | |
TypeError: Cannot set property ip of [object Object] which has only a getter | |
at _ipAsRemoteAddress (/Users/shwei/Work/workspace/cf-fastify-restify-express/functions/node_modules/fastify/fastify.js:249:12) | |
*/ | |
req = Object.assign({ip: ''}, {...req}); | |
fastify.ready(err => { | |
if (err) throw err; | |
handleRequest(req, res); | |
}); | |
}; |
I get this error instead.
Using with Fastify v2.14.0.
\node_modules\fastify\lib\contentTypeParser.js:137
req.setEncoding('utf8')
^
TypeError: req.setEncoding is not a function
at rawBody (D:\my-projects\my-service\node_modules\fastify\lib\contentTypeParser.js:137:9)
at ContentTypeParser.run (D:\my-projects\my-service\node_modules\fastify\lib\contentTypeParser.js:95:7)
at handleRequest (D:\my-projects\my-service\node_modules\fastify\lib\handleRequest.js:35:39)
at onRunMiddlewares (D:\my-projects\my-service\node_modules\fastify\lib\middleware.js:22:5)
at middlewareCallback (D:\my-projects\my-service\node_modules\fastify\lib\route.js:381:5)
at Object.routeHandler [as handler] (D:\my-projects\my-service\node_modules\fastify\lib\route.js:359:7)
at Router.lookup (D:\my-projects\my-service\node_modules\find-my-way\index.js:349:14)
at D:\my-projects\my-service\lib\sendNotification.js:46:9
at Object._encapsulateThreeParam (D:\my-projects\my-service\node_modules\avvio\boot.js:450:13)
at Boot.timeoutCall (D:\my-projects\my-service\node_modules\avvio\boot.js:370:5)
Any Suggestion/help how to send data in a post request, even defining a schema also is not helping???
Above example works only if you don't send any json data in body. Once you send the it gives error:
payload.setEncoding('utf8')
^
TypeError: payload.setEncoding is not a function
at rawBody (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/lib/contentTypeParser.js:150:13)
at ContentTypeParser.run (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/lib/contentTypeParser.js:101:5)
at handleRequest (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/lib/handleRequest.js:35:39)
at runPreParsing (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/lib/route.js:385:5)
at Object.routeHandler [as handler] (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/lib/route.js:343:7)
at Router.lookup (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/find-my-way/index.js:356:14)
at /Users/manasaranjanmishra/rize/fastify-env/functions/src/index.js:39:5
at manageErr (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/fastify.js:392:11)
at exit (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/lib/hooks.js:90:5)
at next (/Users/manasaranjanmishra/rize/fastify-env/functions/node_modules/fastify/lib/hooks.js:101:9)
Any Suggestion/help how to send data in a post request, even defining a schema also is not helping.
fastify/fastify#946