我的练习库和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' ,
} ,
} ,
} ) 可以在此处找到核心和社区插件列表。该项目使用了以下插件和软件包。
准备开始了吗?