fatsify核心功能示例测试!!!
This commit is contained in:
44
node_modules/fastify/examples/benchmark/hooks-benchmark-async-await.js
generated
vendored
Normal file
44
node_modules/fastify/examples/benchmark/hooks-benchmark-async-await.js
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
'use strict'
|
||||
|
||||
const fastify = require('../../fastify')({ logger: false })
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function promiseFunction (resolve) {
|
||||
setImmediate(resolve)
|
||||
}
|
||||
|
||||
async function asyncHook () {
|
||||
await new Promise(promiseFunction)
|
||||
}
|
||||
|
||||
fastify
|
||||
.addHook('onRequest', asyncHook)
|
||||
.addHook('onRequest', asyncHook)
|
||||
.addHook('preHandler', asyncHook)
|
||||
.addHook('preHandler', asyncHook)
|
||||
.addHook('preHandler', asyncHook)
|
||||
.addHook('onSend', asyncHook)
|
||||
|
||||
fastify.get('/', opts, function (request, reply) {
|
||||
reply.send({ hello: 'world' })
|
||||
})
|
||||
|
||||
fastify.listen({ port: 3000 }, function (err) {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user