1 | 'use strict'
|
2 |
|
3 | const fastify = require('../fastify')({ logger: true })
|
4 |
|
5 | const opts = {
|
6 | schema: {
|
7 | response: {
|
8 | '2xx': {
|
9 | type: 'object',
|
10 | properties: {
|
11 | hello: {
|
12 | type: 'string'
|
13 | }
|
14 | }
|
15 | }
|
16 | }
|
17 | }
|
18 | }
|
19 |
|
20 | const optsPost = {
|
21 | schema: {
|
22 | body: {
|
23 | type: 'object',
|
24 | required: ['hello'],
|
25 | properties: {
|
26 | hello: {
|
27 | type: 'string'
|
28 | }
|
29 | }
|
30 | },
|
31 | response: opts.response
|
32 | }
|
33 | }
|
34 |
|
35 | fastify
|
36 | .addHook('onRequest', function (request, reply, done) {
|
37 | console.log('onRequest')
|
38 | done()
|
39 | })
|
40 | .addHook('preParsing', function (request, reply, payload, done) {
|
41 | console.log('preParsing')
|
42 | done()
|
43 | })
|
44 | .addHook('preValidation', function (request, reply, done) {
|
45 | console.log('preValidation')
|
46 | done()
|
47 | })
|
48 | .addHook('preHandler', function (request, reply, done) {
|
49 | console.log('preHandler')
|
50 | done()
|
51 | })
|
52 | .addHook('preSerialization', function (request, reply, payload, done) {
|
53 | console.log('preSerialization', payload)
|
54 | done()
|
55 | })
|
56 | .addHook('onError', function (request, reply, error, done) {
|
57 | console.log('onError', error.message)
|
58 | done()
|
59 | })
|
60 | .addHook('onSend', function (request, reply, payload, done) {
|
61 | console.log('onSend', payload)
|
62 | done()
|
63 | })
|
64 | .addHook('onResponse', function (request, reply, done) {
|
65 | console.log('onResponse')
|
66 | done()
|
67 | })
|
68 | .addHook('onRoute', function (routeOptions) {
|
69 | console.log('onRoute')
|
70 | })
|
71 | .addHook('onListen', async function () {
|
72 | console.log('onListen')
|
73 | })
|
74 | .addHook('onClose', function (instance, done) {
|
75 | console.log('onClose')
|
76 | done()
|
77 | })
|
78 |
|
79 | fastify.get('/', opts, function (req, reply) {
|
80 | reply.send({ hello: 'world' })
|
81 | })
|
82 |
|
83 | fastify.post('/', optsPost, function (req, reply) {
|
84 | reply.send({ hello: 'world' })
|
85 | })
|
86 |
|
87 | fastify.listen({ port: 3000 }, function (err) {
|
88 | if (err) {
|
89 | throw err
|
90 | }
|
91 | })
|