您现在的位置是:群英 > 开发技术 > web开发
Fastify框架的特点是什么,如何应用呢
Admin发表于 2022-09-06 17:48:30857 次浏览
这篇文章主要为大家详细介绍了Fastify框架的特点是什么,如何应用呢,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望对大家学习或工作能有帮助。



前端的web框架,大部分都是建立在node基础上的。fastify 也不例外。

前端web框架性能比对

如果真的是这样的话,那么是很乐意去尝试fastfy的 ??

Benchmarks

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

Fastify支持的特性

  • 高性能: 请见上表.
  • Extensible: 通过 hooks, plugins and decorators 来实现扩展性.
  • Schema based: 不强制使用 JSON Schema 验证你的路由配置,及时配置了,编译也是很快的.
  • Logging: 使用Pino来记录日志,并把损耗降低。
  • Developer friendly: 对开发者友好,而且对性能、安全性也有考虑、设计.
  • TypeScript ready: 支持 TypeScript

Fastify支持的 plugins

截止到目前, 48个核心插件 、179个社区插件

那么,如何使用呢?

初始化

创建工程

npm install --global fastify-cli
fastify generate myproject

初始化工程

npm init -y fastify

安装依赖

#npm 
npm i fastify

#yarn 
yarn add fastify

hello-world

同步返回

// 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}
})

plugin如何使用

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
})

更多使用

  • Example List
  • Getting Started
  • Guides
  • Server
  • Routes
  • Encapsulation
  • Logging
  • Middleware
  • Hooks
  • Decorators
  • Validation and Serialization
  • Fluent Schema
  • Lifecycle
  • Reply
  • Request
  • Errors
  • Content Type Parser
  • Plugins
  • Testing
  • Benchmarking
  • How to write a good plugin
  • Plugins Guide
  • HTTP2
  • Long Term Support
  • TypeScript and types support
  • Serverless
  • Recommendations

相关link

  • #json schema

  • #pino



以上就是关于Fastify框架的特点是什么,如何应用呢的介绍啦,需要的朋友可以参考上述内容,希望对大家有帮助,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: Fastify
相关信息推荐
2022-12-14 11:05:39 
摘要:乐观锁(optimistic locking)表现出大胆、务实的态度。使用乐观锁的前提是, 实际应用当中,发生冲突的概率比较低。他的设计和实现直接而简洁。 目前Web应用中,乐观锁的使用占有绝对优势。悲观锁正如其名字,悲观锁(pessimistic locking)体现了一种谨慎的处事态度
2022-01-26 18:39:37 
摘要:Java怎样计算日期差,有什么方法?
2021-12-02 17:41:17 
摘要:今天我们一起来了解一下Goto语句在PHP中的应用,在C++、Java及很多语言中都有goto语句,它的使用是直接去到某个地方。PHP中也有这个功能,接下来我们就来看看Goto语句在PHP中怎样使用。
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部