這篇文章跟大家分享一個Nodejs web框架:Fastify,簡單介紹一下Fastify支援的特性、Fastify支援的外掛程式以及Fastify的使用方法,希望對大家有幫助!
前端的web框架,大部分都是建立在node基礎上的。 fastify 也不例外。
如果真的是這樣的話,那麼是很樂意去嘗試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
##Framework | |||
Router? | Requests/sec | ||
4.17.3 | ✓ | 14,200 | |
#20.2.1 | ✓42,284 | Restify | 8.6.1##✓50,363 |
#Koa | 2.13.0 | ✗
54,272 |
|
#4.0.0 |
✓ |
##截止到目前,48個核心外掛、179個社群外掛程式
建立工程
npm install --global fastify-cli fastify generate myproject
初始化工程
npm init -y fastify
安裝依賴
#npm npm i fastify #yarn yarn add fastify
#同步回傳
// 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} })
非同步返回
// 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]),更多的使用用法,可以點擊鏈接類似下發,跳到連結進嘗試~
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 教程!
以上是分享一個Nodejs web框架:Fastify的詳細內容。更多資訊請關注PHP中文網其他相關文章!