fatsify核心功能示例测试!!!
This commit is contained in:
3
node_modules/fastify/examples/benchmark/body.json
generated
vendored
Normal file
3
node_modules/fastify/examples/benchmark/body.json
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"hello": "world"
|
||||
}
|
||||
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
|
||||
}
|
||||
})
|
||||
52
node_modules/fastify/examples/benchmark/hooks-benchmark.js
generated
vendored
Normal file
52
node_modules/fastify/examples/benchmark/hooks-benchmark.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
'use strict'
|
||||
|
||||
const fastify = require('../../fastify')({ logger: false })
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify
|
||||
.addHook('onRequest', function (request, reply, done) {
|
||||
done()
|
||||
})
|
||||
.addHook('onRequest', function (request, reply, done) {
|
||||
done()
|
||||
})
|
||||
|
||||
fastify
|
||||
.addHook('preHandler', function (request, reply, done) {
|
||||
done()
|
||||
})
|
||||
.addHook('preHandler', function (request, reply, done) {
|
||||
setImmediate(done)
|
||||
})
|
||||
.addHook('preHandler', function (request, reply, done) {
|
||||
done()
|
||||
})
|
||||
|
||||
fastify
|
||||
.addHook('onSend', function (request, reply, payload, done) {
|
||||
done()
|
||||
})
|
||||
|
||||
fastify.get('/', opts, function (request, reply) {
|
||||
reply.send({ hello: 'world' })
|
||||
})
|
||||
|
||||
fastify.listen({ port: 3000 }, function (err) {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
47
node_modules/fastify/examples/benchmark/parser.js
generated
vendored
Normal file
47
node_modules/fastify/examples/benchmark/parser.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
|
||||
const fastify = require('../../fastify')({
|
||||
logger: false
|
||||
})
|
||||
|
||||
const jsonParser = require('fast-json-body')
|
||||
const querystring = require('node:querystring')
|
||||
|
||||
// Handled by fastify
|
||||
// curl -X POST -d '{"hello":"world"}' -H'Content-type: application/json' http://localhost:3000/
|
||||
|
||||
// curl -X POST -d '{"hello":"world"}' -H'Content-type: application/jsoff' http://localhost:3000/
|
||||
fastify.addContentTypeParser('application/jsoff', function (request, payload, done) {
|
||||
jsonParser(payload, function (err, body) {
|
||||
done(err, body)
|
||||
})
|
||||
})
|
||||
|
||||
// curl -X POST -d 'hello=world' -H'Content-type: application/x-www-form-urlencoded' http://localhost:3000/
|
||||
fastify.addContentTypeParser('application/x-www-form-urlencoded', function (request, payload, done) {
|
||||
let body = ''
|
||||
payload.on('data', function (data) {
|
||||
body += data
|
||||
})
|
||||
payload.on('end', function () {
|
||||
try {
|
||||
const parsed = querystring.parse(body)
|
||||
done(null, parsed)
|
||||
} catch (e) {
|
||||
done(e)
|
||||
}
|
||||
})
|
||||
payload.on('error', done)
|
||||
})
|
||||
|
||||
// curl -X POST -d '{"hello":"world"}' -H'Content-type: application/vnd.custom+json' http://localhost:3000/
|
||||
fastify.addContentTypeParser(/^application\/.+\+json$/, { parseAs: 'string' }, fastify.getDefaultJsonParser('error', 'ignore'))
|
||||
|
||||
fastify
|
||||
.post('/', function (req, reply) {
|
||||
reply.send(req.body)
|
||||
})
|
||||
|
||||
fastify.listen({ port: 3000 }, (err, address) => {
|
||||
if (err) throw err
|
||||
})
|
||||
30
node_modules/fastify/examples/benchmark/simple.js
generated
vendored
Normal file
30
node_modules/fastify/examples/benchmark/simple.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const fastify = require('../../fastify')({
|
||||
logger: false
|
||||
})
|
||||
|
||||
const schema = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify
|
||||
.get('/', schema, function (req, reply) {
|
||||
reply
|
||||
.send({ hello: 'world' })
|
||||
})
|
||||
|
||||
fastify.listen({ port: 3000 }, (err, address) => {
|
||||
if (err) throw err
|
||||
})
|
||||
Reference in New Issue
Block a user