1 | 'use strict'
|
2 |
|
3 | const fastify = require('../fastify')()
|
4 | const jsonParser = require('fast-json-body')
|
5 | const qs = require('qs')
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | fastify.addContentTypeParser('application/jsoff', function (request, payload, done) {
|
12 | jsonParser(payload, function (err, body) {
|
13 | done(err, body)
|
14 | })
|
15 | })
|
16 |
|
17 |
|
18 | fastify.addContentTypeParser('application/x-www-form-urlencoded', function (request, payload, done) {
|
19 | var body = ''
|
20 | payload.on('data', function (data) {
|
21 | body += data
|
22 | })
|
23 | payload.on('end', function () {
|
24 | try {
|
25 | const parsed = qs.parse(body)
|
26 | done(null, parsed)
|
27 | } catch (e) {
|
28 | done(e)
|
29 | }
|
30 | })
|
31 | payload.on('error', done)
|
32 | })
|
33 |
|
34 | fastify
|
35 | .post('/', function (req, reply) {
|
36 | reply.send(req.body)
|
37 | })
|
38 |
|
39 | fastify.listen(3000, err => {
|
40 | if (err) throw err
|
41 | console.log(`server listening on ${fastify.server.address().port}`)
|
42 | })
|