本篇文章给大家分享一个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 | 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 |
| - | |||
http.Server |
16.14.2 | ✗ | 74,513 |
截止到目前, 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 StartedGuides更多node相关知识,请访问:JSON Schema6!
以上就是分享一个Nodejs web框架:Fastify的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号