温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nodejs web框架Fastify怎么使用

发布时间:2022-08-05 09:48:15 来源:亿速云 阅读:178 作者:iii 栏目:web开发

这篇文章主要介绍了Nodejs web框架Fastify怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Nodejs web框架Fastify怎么使用文章都会有所收获,下面我们一起来看看吧。

Nodejs web框架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

FrameworkVersionRouter?Requests/sec
Express4.17.314,200
hapi20.2.142,284
Restify8.6.150,363
Koa2.13.054,272
Fastify4.0.077,193
-


http.Server16.14.274,513

Fastify支持的特性

  • 高性能:  请见上表.

  • Extensible:  通过 hooks, plugins and decorators 来实现扩展性.

  • Schema based:  不强制使用 JSON Schema 验证你的路由配置,及时配置了,编译也是很快的.

  • Logging:  使用Pino来记录日志,并把损耗降低。

  • Developer friendly:  对开发者友好,而且对性能、安全性也有考虑、设计.

  • TypeScript ready: 支持 TypeScript

Fastify支持的 plugins

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

Nodejs web框架Fastify怎么使用

那么,如何使用呢?

初始化

创建工程

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]),更多的使用用法, 可以点击链接类似下发,跳转链接进尝试~

Nodejs web框架Fastify怎么使用

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

关于“Nodejs web框架Fastify怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Nodejs web框架Fastify怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

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

AI