Home > Web Front-end > JS Tutorial > body text

Share a Nodejs web framework: Fastify

青灯夜游
Release: 2022-08-04 21:23:15
forward
2799 people have browsed it

This article will share with you a Nodejs web framework: Fastify. I will briefly introduce the features supported by Fastify, the plug-ins supported by Fastify, and how to use Fastify. I hope it will be helpful to everyone!

Share a Nodejs web framework: Fastify

Most of the front-end web frameworks are based on node. fastify is no exception.

Front-end web framework performance comparison

If this is really the case, then would you be happy to try 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

-#http.Server

Features supported by Fastify

  • High performance: Please see the table above.
  • Extensible: Achieve scalability through hooks, plugins and decorators.
  • Schema based: It is not mandatory to use JSON Schema Verify your routing configuration, configure it in time, and compile it easily Fast.
  • Logging: Use Pino to record logs and reduce losses.
  • Developer friendly: It is developer friendly, and it also considers and designs performance and security.
  • TypeScript ready: Support TypeScript

Fastify supports plugins

As of now, 48 Core plug-ins, 179 community plug-ins

Share a Nodejs web framework: Fastify

So, how to use it?

Initialization

Create project

npm install --global fastify-cli
fastify generate myproject
Copy after login

Initialize project

npm init -y fastify
Copy after login

Installation dependencies

#npm 
npm i fastify

#yarn 
yarn add fastify
Copy after login

hello-world

##Return synchronously

// 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}
})
Copy after login

Asynchronous return

// 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}
})
Copy after login

How to use plugin

fastify.register(plugin, [options]), for more usage, you can click the link Similar to delivery, try jumping to the link~

Share a Nodejs web framework: 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
})
Copy after login

更多使用

相关link

更多node相关知识,请访问:nodejs 教程

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



16.14.2 74,513

The above is the detailed content of Share a Nodejs web framework: Fastify. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!