Table of Contents
Front-end web framework performance comparison
Benchmarks
Features supported by Fastify
So, how to use it?
Initialization
hello-world
更多使用
相关link
Home Web Front-end JS Tutorial Share a Nodejs web framework: Fastify

Share a Nodejs web framework: Fastify

Aug 04, 2022 pm 09:23 PM
node.js node web framework fastify

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

更多使用

更多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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Build web applications using Golang's web framework Buffalo Build web applications using Golang's web framework Buffalo Jun 24, 2023 am 10:27 AM

Buffalo is a web framework developed using Golang that provides a solution for rapid development of web applications. In this article, we will introduce how to use Buffalo to build a web application. Installing Buffalo First, we need to install Buffalo locally. Buffalo provides a convenient command line tool through which you can build and run applications. Before installing, make sure you have Golang and Node.js installed. Ran

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Go language web framework comparison: gin vs. echo vs. iris Go language web framework comparison: gin vs. echo vs. iris Jun 17, 2023 pm 07:44 PM

As the demand for web development continues to increase, web frameworks in various languages ​​​​are gradually diversified, and the Go language is no exception. Among the many web frameworks in Go language, gin, echo and iris are the three most popular frameworks. In this article, we will compare the pros and cons of these three frameworks to help you choose the right one for your project. Gingin is a lightweight web framework that features high performance and flexibility. It supports middleware and routing functionality, which makes it ideal for building RESTful

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to implement a lightweight web framework using PHP and Slim How to implement a lightweight web framework using PHP and Slim Jun 25, 2023 pm 01:03 PM

Web frameworks have become an integral part of modern web application development, providing an infrastructure that enables developers to create and deploy their applications faster. In PHP development, Slim is a lightweight web framework known for its ease of use and rapid development. This article will show you how to create a simple but powerful web application using PHP and Slim. What is Slim? Slim is a lightweight web framework written in the language PHP. Its core

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package

Token-based authentication with Angular and Node Token-based authentication with Angular and Node Sep 01, 2023 pm 02:01 PM

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to

See all articles