我的練習庫和JSNSD認證的註釋。此倉庫中涵蓋了以下主題:
Fastify文檔的入門指南是創建基本服務器的絕佳資源。
如果您想快速生成一個快速項目
npm init fastify
# install the dependencies
npm install
# run the dev server
npm run dev請訪問路線http://127.0.0.1:3000/01-basic 3000/01-basic在瀏覽器上進行基本的HTML響應。
將快速整合到現有項目中:
npm init fastify -- --integrate安裝快速靜態並配置特定目錄以服務。
npm install --save fastify-static在app.js中配置
const fastifyStatic = require ( 'fastify-static' )
/** @type import('fastify').FastifyPluginAsync */
module . exports = async function ( fastify , opts ) {
// Place here your custom code!
fastify . register ( fastifyStatic , {
root : path . join ( __dirname , 'public' ) ,
prefix : '/pub/' ,
} )訪問http://127.0.0.1:3000/02-static-content 3000/02-STATIC-CONTENT,用於自定義文件和http://127.0.0.1:3000/pub/about.html ,因為公共目錄已安裝在/pub/ path上。
安裝必要的軟件包。閱讀有關觀點和車把的更多信息。
npm install point-of-view handlebars創建一個view目錄並配置應用程序
const pointOfView = require ( 'point-of-view' )
const handlebars = require ( 'handlebars' )
/** @type import('fastify').FastifyPluginAsync */
module . exports = async function ( fastify , opts ) {
fastify . register ( pointOfView , {
engine : { handlebars } ,
root : path . join ( __dirname , 'views' ) ,
layout : 'layout.hbs' ,
} )根據此處建立路線,然後訪問http://127.0.0.1:3000/03-render-views?name=dina
一種基本的待辦事項服務,將允許CRUD操作,並將與SQLite數據庫進行交互以進行持久。
npm install sequelize sqlite3添加了一個插件,該插件將使用models功能裝飾fastify實例。它也可以是一個對象。
const fp = require ( 'fastify-plugin' )
const { db } = require ( '../db' )
// initialize models
const makeTodoModel = require ( '../models/todo' )
const Todo = makeTodoModel ( db )
module . exports = fp ( async function ( fastify , opts ) {
fastify . decorate ( 'models' , function ( ) {
return { db , Todo }
} )
} )現在您可以在路由中使用該功能
module . exports = async ( fastify , opts ) => {
const { Todo } = fastify . models ( )
fastify . get ( '/' , async ( request , reply ) => {
try {
const todos = await Todo . findAll ( )
return todos . map ( ( t ) => t . toJSON ( ) )
} catch ( error ) {
throw error
}
} ) 為此,我們在主要應用程序中消費了Rick和Morty API。
安裝了HTTP客戶got ,以便於我們的應用程序。
npm i got
# define following env variables that will be used by the service.
# injecting variables this way is normal for consuming from dynamic dependent
# services
export CHARACTER_SERVICE=https://rickandmortyapi.com/api/character
export LOCATION_SERVICE=https://rickandmortyapi.com/api/location
使用文件./aggregate-test.http測試API。
安裝所需的軟件包
npm install fastify-reply-from fastify-http-proxy單路線:多起點代理
註冊插件並定義將通過查詢參數傳遞的URL返迴響應的路由。插件fastify-reply-from接受URL的函數from reply對象。
fastify . get ( '/' , async function ( request , reply ) {
const { url } = request . query
try {
new URL ( url )
} catch ( err ) {
throw fastify . httpErrors . badRequest ( )
}
return reply . from ( url )
} )訪問URL: http://127.0.0.1:3000/06-proxy/?url=https://github.com/fastify :3000/06-proxy/?url=https://github.com/fastify
使用HTTP代理:單一原始,多路由代理
註冊下面的插件,然後傳遞配置對象。以下示例中的代理將安裝在/rickandmorty上。
fastify . register ( require ( 'fastify-http-proxy' ) , {
upstream : 'https://rickandmortyapi.com/api/episode' ,
prefix : '/rickandmorty' ,
} )訪問http://127.0.0.1:3000/rickandmorty/3現在是https://rickandmortyapi.com/api/episode/3
參數污染:提供同名的多個HTTP參數可能會導致應用程序以意外的方式解釋值。通過利用這些效果,攻擊者可能能夠繞過輸入驗證,觸發應用程序錯誤或修改內部變量值。由於HTTP參數污染(簡短HPP)會影響所有Web技術,服務器和客戶端攻擊的構建塊。
在第一個路線/nameType中,我們處理了3例查詢字符串污染。
name為字符串)name為array)name )只要告訴Fastify使用JSON Schema驗證傳入的請求,更容易。只需將選項對象與模式添加
{
schema : {
params : {
id : { type : 'number' } ,
} ,
body : {
type : 'object' ,
required : [ 'name' ] ,
additionalProperties : false ,
properties : {
name : { type : 'string' } ,
} ,
} ,
} ,
} ,我們引入了驗證是第二個路線/withValidation
name ,不良請求)name可以是數組或字符串)使用additionalProperties: false在模式中可以剝離被傳遞給處理程序的不必要的屬性。
驗證也可以用於響應。模式中只有提到的屬性才會暴露。如果響應不符合架構誤差,則會引發。
{
schema : {
response : {
201 : {
posted : { type : 'boolean' } ,
hello : { type : 'string' } ,
} ,
} ,
} ,
} ,流利的Schema也可以用來定義這些模式。
幾乎是安全的,但是假設某人或某個人正在用隨機請求(DOS)轟炸我們,並導致我們的系統減慢或刮擦我們的服務。為了使他們的生活更加艱難,我們可以阻止他們在服務器中的IP。讓我們看看我們如何真正快速地做到這一點。
我們可以創建一個插件並使用快速掛鉤來實現此目標。
fastify . addHook ( 'onRequest' , async function ( request , reply ) {
if ( request . ip === '127.0.0.1' ) {
return reply . forbidden ( )
}
} ) 魔術是由Fastify-Swagger插件從路由模式自動生成Swagger(OpenAPI V2)或OpenAPI V3模式的Fastify-Swagger插件來完成的。註冊插件,它可以正常工作:
fastify . register ( require ( 'fastify-swagger' ) , {
routePrefix : '/doc' ,
exposeRoute : true ,
openapi : {
openapi : '3.0.3' ,
info : {
title : 'JSNSD' ,
description : 'Testing the Fastify swagger API' ,
version : '0.1.0' ,
} ,
externalDocs : {
url : 'https://swagger.io' ,
description : 'Find more info here' ,
} ,
} ,
} ) 可以在此處找到核心和社區插件列表。該項目使用了以下插件和軟件包。
準備開始了嗎?