溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nodejs web框架Fastify怎么使用

發(fā)布時(shí)間:2022-08-05 09:48:15 來源:億速云 閱讀:198 作者:iii 欄目:web開發(fā)

這篇文章主要介紹了Nodejs web框架Fastify怎么使用的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Nodejs web框架Fastify怎么使用文章都會(huì)有所收獲,下面我們一起來看看吧。

Nodejs web框架Fastify怎么使用

前端的web框架,大部分都是建立在node基礎(chǔ)上的。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.3?14,200
hapi20.2.1?42,284
Restify8.6.1?50,363
Koa2.13.0?54,272
Fastify4.0.0?77,193
-


http.Server16.14.2?74,513

Fastify支持的特性

  • 高性能:  請見上表.

  • Extensible:  通過 hooks, plugins and decorators 來實(shí)現(xiàn)擴(kuò)展性.

  • Schema based:  不強(qiáng)制使用 JSON Schema 驗(yàn)證你的路由配置,及時(shí)配置了,編譯也是很快的.

  • Logging:  使用Pino來記錄日志,并把損耗降低。

  • Developer friendly:  對開發(fā)者友好,而且對性能、安全性也有考慮、設(shè)計(jì).

  • TypeScript ready: 支持 TypeScript

Fastify支持的 plugins

截止到目前, 48個(gè)核心插件 、179個(gè)社區(qū)插件

Nodejs web框架Fastify怎么使用

那么,如何使用呢?

初始化

創(chuàng)建工程

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]),更多的使用用法, 可以點(diǎn)擊鏈接類似下發(fā),跳轉(zhuǎn)鏈接進(jìn)嘗試~

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

相關(guān)link

  • #json schema

  • #pino

關(guān)于“Nodejs web框架Fastify怎么使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Nodejs web框架Fastify怎么使用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI