UNPKG

1.86 kBJavaScriptView Raw
1'use strict'
2
3const fastify = require('../fastify')()
4
5const opts = {
6 schema: {
7 response: {
8 '2xx': {
9 type: 'object',
10 properties: {
11 hello: {
12 type: 'string'
13 }
14 }
15 }
16 }
17 }
18}
19
20const 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
35fastify
36 .addHook('onRequest', function (request, reply, next) {
37 console.log('onRequest')
38 next()
39 })
40 .addHook('preParsing', function (request, reply, next) {
41 console.log('preParsing')
42 next()
43 })
44 .addHook('preValidation', function (request, reply, next) {
45 console.log('preValidation')
46 next()
47 })
48 .addHook('preHandler', function (request, reply, next) {
49 console.log('preHandler')
50 next()
51 })
52 .addHook('preSerialization', function (request, reply, payload, next) {
53 console.log('preSerialization', payload)
54 next()
55 })
56 .addHook('onError', function (request, reply, error, next) {
57 console.log('onError', error.message)
58 next()
59 })
60 .addHook('onSend', function (request, reply, payload, next) {
61 console.log('onSend', payload)
62 next()
63 })
64 .addHook('onResponse', function (request, reply, next) {
65 console.log('onResponse')
66 next()
67 })
68 .addHook('onRoute', function (routeOptions) {
69 console.log('onRoute')
70 })
71 .addHook('onClose', function (instance, done) {
72 console.log('onClose')
73 done()
74 })
75
76fastify.get('/', opts, function (req, reply) {
77 reply.send({ hello: 'world' })
78})
79
80fastify.post('/', optsPost, function (req, reply) {
81 reply.send({ hello: 'world' })
82})
83
84fastify.listen(3000, function (err) {
85 if (err) {
86 throw err
87 }
88 console.log(`server listening on ${fastify.server.address().port}`)
89})