1 | 'use strict'
|
2 |
|
3 | const fastify = require('../fastify')()
|
4 |
|
5 | const opts = {
|
6 | schema: {
|
7 | response: {
|
8 | 200: {
|
9 | type: 'object',
|
10 | properties: {
|
11 | hello: {
|
12 | type: 'string'
|
13 | }
|
14 | }
|
15 | }
|
16 | }
|
17 | }
|
18 | }
|
19 |
|
20 | function promiseFunction (resolve) {
|
21 | setImmediate(resolve)
|
22 | }
|
23 |
|
24 | async function asyncHook () {
|
25 | await new Promise(promiseFunction)
|
26 | }
|
27 |
|
28 | fastify
|
29 | .addHook('onRequest', asyncHook)
|
30 | .addHook('onRequest', asyncHook)
|
31 | .addHook('preHandler', asyncHook)
|
32 | .addHook('preHandler', asyncHook)
|
33 | .addHook('preHandler', asyncHook)
|
34 | .addHook('onSend', asyncHook)
|
35 |
|
36 | fastify.get('/', opts, function (request, reply) {
|
37 | reply.send({ hello: 'world' })
|
38 | })
|
39 |
|
40 | fastify.listen(3000, function (err) {
|
41 | if (err) {
|
42 | throw err
|
43 | }
|
44 | console.log(`server listening on ${fastify.server.address().port}`)
|
45 | })
|