-
-
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); | |
| }); | |
| }; |
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.
I get this error instead.
Using with Fastify v2.14.0.