Home>Article>Web Front-end> Share a Nodejs web framework: Fastify
This article will share with you aNodejsweb framework: Fastify. I will briefly introduce the features supported by Fastify, the plug-ins supported by Fastify, and how to use Fastify. I hope it will be helpful to everyone!
Most of the front-end web frameworks are based onnode.fastifyis no exception.
If this is really the case, then would you be happy to try fastfy? ?
Machine:EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD.
Method::autocannon -c 100 -d 40 -p 10 localhost:3000
* 2, taking the second average
As of now, 48 Core plug-ins, 179 community plug-ins
Create project
npm install --global fastify-cli fastify generate myproject
Initialize project
npm init -y fastify
Installation dependencies
#npm npm i fastify #yarn yarn add fastify
##Return synchronously
// ESM import Fastify from 'fastify' //const fastify = Fastify({ //logger: true //}) // CommonJs const fastify = require('fastify')({ logger: true }) // Declare a route fastify.get('/', (request, reply) => { reply.send({ hello: 'world' }) }) // Run the server! fastify.listen({ port: 3000 }, (err, address) => { if (err) throw err // Server is now listening on ${address} })
Asynchronous return
// ESM import Fastify from 'fastify' const fastify = Fastify({ logger: true }) // CommonJs //const fastify = require('fastify')({ //logger: true //}) fastify.get('/', async (request, reply) => { reply.type('application/json').code(200) return { hello: 'world' } }) fastify.listen({ port: 3000 }, (err, address) => { if (err) throw err // Server is now listening on ${address} })
fastify.register(plugin, [options]), for more usage, you can click the link Similar to delivery, try jumping to the link~
const fastifySession = require('fastify-session') fastify.register(fastifySession, { cookieName: 'sessionId', secret: 'a secret with minimum length of 32 characters', cookie: { secure: false }, expires: 1800000 })
Getting Started
Guides
Server
Encapsulation
Logging
Middleware
Decorators
Validation and Serialization
Fluent Schema
Reply
Errors
Content Type Parser
Testing
How to write a good plugin
Plugins Guide
HTTP2
Long Term Support
TypeScript and types support
Recommendations
更多node相关知识,请访问:nodejs 教程!
Framework | Version | Router? | Requests/sec |
---|---|---|---|
Express | 4.17.3 | ✓ | 14,200 |
hapi | 20.2.1 | ✓ | 42,284 |
Restify | 8.6.1 | ✓ | 50,363 |
Koa | 2.13.0 | ✗ | 54,272 |
Fastify | 4.0.0 | ✓ | ##77,193 |
16.14.2 |
✗ | 74,513 |
The above is the detailed content of Share a Nodejs web framework: Fastify. For more information, please follow other related articles on the PHP Chinese website!