UNPKG

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