1 | 'use strict'
|
2 |
|
3 | const fastify = require('../fastify')({ logger: true })
|
4 | const jsonParser = require('fast-json-body')
|
5 | const querystring = require('node:querystring')
|
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 | let body = ''
|
20 | payload.on('data', function (data) {
|
21 | body += data
|
22 | })
|
23 | payload.on('end', function () {
|
24 | try {
|
25 | const parsed = querystring.parse(body)
|
26 | done(null, parsed)
|
27 | } catch (e) {
|
28 | done(e)
|
29 | }
|
30 | })
|
31 | payload.on('error', done)
|
32 | })
|
33 |
|
34 |
|
35 | fastify.addContentTypeParser(/^application\/.+\+json$/, { parseAs: 'string' }, fastify.getDefaultJsonParser('error', 'ignore'))
|
36 |
|
37 |
|
38 |
|
39 | fastify.removeContentTypeParser('application/json')
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | fastify
|
45 | .post('/', function (req, reply) {
|
46 | reply.send(req.body)
|
47 | })
|
48 |
|
49 | fastify.listen({ port: 3000 }, err => {
|
50 | if (err) {
|
51 | throw err
|
52 | }
|
53 | })
|