您的位置: 翼速应用 > 业内知识 > web前端 > 正文

Nodejs web框架:Fastify之详解

本文是关于Nodejs web框架:Fastify之详解,主要介绍下Fastify支持的特性、Fastify支持的插件以及Fastify的使用方法,下面一起来看一下。


Nodejs web框架:Fastify之详解


Nodejs web框架:Fastify之详解


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


Benchmarks


Fastify支持的特性


●  高性能: 请见上表.

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

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

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

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

●  TypeScript ready: 支持 TypeScript


Fastify支持的 plugins


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


Fastify支持的 plugins


那么,如何使用呢?


初始化


创建工程


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


plugin更多使用方法


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之详解,翼速应用平台内有更多相关资讯,欢迎查阅!

我来说两句

0 条评论

推荐阅读

  • 响应式布局CSS媒体查询设备像素比介绍

    构建响应式网站布局最常见的是流体网格,灵活调整大小的站点布局技术,确保用户在使用的幕上获得完整的体验。响应式设计如何展示富媒体图像,可以通过以下几种方法。

    admin
  • 提升网站的性能快速加载的实用技巧

    网站速度很重要,快速加载的网站会带来更好的用户体验、更高的转化率、更多的参与度,而且在搜索引擎排名中也扮演重要角色,做SEO,网站硬件是起跑线,如果输在了起跑线,又怎么跟同行竞争。有许多方法可提升网站的性能,有一些技巧可以避免踩坑。

    admin
  • 织梦CMS TAG页找不到标签和实现彩色标签解决方法

    织梦cms是我们常见的网站程序系统的一款,在TAG标签中常常遇到的问题也很多。当我们点击 tags.php 页的某个标签的时候,有时会提示:“系统无此标签,可 能已经移除!” 但是我们检查程序后台,以及前台显示页面。这个标签确实存在,如果解决这个问题那?

    admin
  • HTML关于fieldset标签主要的作用

    在前端开发html页面中常用的标签很多,今天为大家带来的是关于HTML中fieldset标签主要的作用说明,根据技术分析HTML

    admin

精选专题