Fastify 系列教程二 (中间件、钩子函数和装饰器)
Fastify 系列教程:
- Fastify 系列教程一 (路由和日志)
- Fastify 系列教程二 (中间件、钩子函数和装饰器)
- Fastify 系列教程三 (验证、序列化和生命周期)
- Fastify 系列教程四 (请求对象、响应对象和插件)
中间件
Fastify 提供了与 Express 和 Restify 中间件兼容的异步中间件引擎。
所以,Fastify 可以使用 Express 中间件。
注意:Fastify 中没有错误处理中间件(err, req, res, next)。如果需要处理中间件中的错误时,只需要调用 next(new Error('error message')) 即可,Fastify 会为你关闭请求并发送错误响应。
示例:
访问静态资源
配置 serve-static 访问静态资源。
目录结构:
public
|-css
|-js
|-jquery.js
|-images
views
app.js
app.js:
const serveStatic = require('serve-static')
const path = require('path')
fastify.use('/public', serveStatic(path.resolve(__dirname, 'public')))
此时,访问 localhost:3030/public/js/jquery.js 可以正确获得静态资源文件。
注意:中间件的请求和响应参数是原生 Nodejs Http 的
req和res,而路由中的request和reply是经过 Fastify 封装的,两者并不一样:
const fastify = require('fastify')()
fastify.use(function(req, res, next){
req.hello = "req.hello"
res.hello = "res.hello"
next()
})
fastify.use(function(req, res, next){
console.log('middle:', req.hello)
console.log('middle:', res.hello)
next()
})
fastify.get('/', function (request, reply) {
console.log('route:', request.hello)
console.log('route:', reply.hello)
reply.send({ hello: 'world' })
})
/*
middle: req.hello
middle: res.hello
route: undefined
route: undefined
*/
钩子函数
Fastify 中共有四个应用级钩子和一个路由级钩子。
四个应用级钩子:
onRequest(req, res, next)preHandler(request, reply, next)onResponse(res, next)onClose(instance, done)
一个路由级钩子:
beforeHandler(request, reply, done)
注意 req、res 和 request、reply 的区别。
| 参数 | 描述 |
|---|---|
| req | Node.js Http 请求对象 |
| res | Node.js Http 响应对象 |
| request | Fastify Request 接口 |
| reply | Fastify Reply 接口 |
| next | 继续下一个 生命周期 任务 |
使用 addHook 方法添加钩子函数:
fastify.addHook('onRequest', (req, res, next) => {
// some code
next()
})
fastify.addHook('preHandler', (request, reply, next) => {
// some code
next()
})
fastify.addHook('onResponse', (res, next) => {
// some code
next()
})
如果在执行钩子函数时遇到错误,只需将其传递给 next(),并且 Fastify 将自动关闭请求并向用户发送相应的错误代码。
fastify.addHook('onRequest', (req, res, next) => {
next(new Error('some error'))
})
如果你希望传递一个自定义状态码,可以使用reply.code():
fastify.addHook('preHandler', (request, reply, next) => {
reply.code(500)
next(new Error('some error'))
})
onClose
onClose 是唯一不在生命周期中的钩子,当调用 fastify.close() 来停止服务器时,会触发此钩子,第一个参数是 Fastify 实例,第二个用来完成回调。
const fastify = require('fastify')()
fastify.get('/close', function(request, reply){
reply.type('text/html').send('<h1>Close Server</h1>')
fastify.close()
})
fastify.onClose(function(instance, done){
console.log('close db connection')
done()
})
访问 /close 时页面会显示 Close Server,并且控制台会输出:
[Running] node "/Users/lavyun/Code/node/learn-fastify/app.js"
close db connection
[Done] exited with code=0 in 8.524 seconds
在关闭数据库链连接之后,app.js 也被 exit 了。
preHandler 和 beforeHandler
preHandler 的受众对象是所有的路由,而 beforeHandler 的受众对象是某个特定的路由,另外,beforeHandler 总是在 preHandler 之后执行。
fastify.addHook('preHandler', (request, reply, done) => {
console.log('preHandler')
done()
})
fastify.get('/', {
beforeHandler: function (request, reply, done) {
console.log('beforeHandler')
done()
}
}, function (request, reply) {
reply.send({ hello: 'world' })
})
// preHandler
// beforeHandler
beforeHandler 也可以是一个函数数组:
fastify.addHook('preHandler', (request, reply, done) => {
console.log('preHandler')
done()
})
const firstBeforeHandler = (request, reply, done) => {
console.log('first beforeHandler')
done()
}
const secondBeforeHandler = (request, reply, done) => {
console.log('second beforeHandler')
done()
}
fastify.get('/', {
beforeHandler: [firstBeforeHandler, secondBeforeHandler]
}, function (request, reply) {
reply.send({ hello: 'world' })
})
// preHandler
// first beforeHandler
// second beforeHandler
装饰器
如果想为 Fastify 实例添加功能,可以使用 decorate 方法。
decorate 允许向 Fastify 实例添加新属性。可以是一个值、一个函数,也可以是一个对象或一个字符串等。
使用方法
decorate
只需要调用 decorate 函数,并且传入新属性的键和值即可。
fastify.decorate('utility', () => {
// something very useful
})
也可以定义其他类型的实例:
fastify.decorate('conf', {
db: 'some.db',
port: 3000
})
一旦定义了这个实例,可以通过传入的参数名称来得到该值:
fastify.utility()
console.log(fastify.conf.db)
装饰器不可以重新被覆盖,如果要定义一个已经存在的装饰器,decorate 将会抛出异常。
fastify.decorate("d1", 'd1')
fastify.decorate("d1", "d2") // Error
decorateReply
decorateReply 可以为 Reply 对象添加新属性。
fastify.decorateReply('utility', function () {
// something very useful
})
decorateRequest
decorateRequest 可以为 Request 对象添加新属性。
fastify.decorateRequest('utility', function () {
// something very useful
})
extendServerError
如果要扩展 服务器错误,可以使用此 API,必须传入一个返回值为对象的函数,该函数将接收原始的 Error 对象,并返回新Error 对象来扩展服务器错误。
fastify.extendServerError((err) => {
return {
timestamp: new Date()
}
})
/*
最终的错误对象格式:
{
error: String
message: String
statusCode: Number
timestamp: Date
}
*/
依赖
如果一个装饰器依赖于另一个装饰器,可以将其他装饰器声明为依赖。只需要添加一个字符串数组(表示所依赖的装饰器的名称)作为第三个参数即可:
fastify.decorate('utility', fn, ['greet', 'log'])
如果不满足依赖关系,那么 decorate 会抛出一个异常,但是不用担心:依赖关系检查会在服务器启动之前执行,所以在运行时不会发生错误。
hasDecorator
可以使用 hasDecorator 检查装饰器是否存在:
fastify.hasDecorator('utility')
Fastify 的更多使用将在接下来的博客中说明。
Tips:
访问 https://lavyun.gitbooks.io/fastify/content/ 查看我翻译的 Fastify 中文文档。
访问 lavyun.cn 查看我的个人动态。
Fastify 系列教程二 (中间件、钩子函数和装饰器)的更多相关文章
- Fastify 系列教程二 (中间件、钩子函数和装饰器)
Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) 中间件 Fastify 提供了与 Express 和 Restify ...
- 【Flask】 python学习第一章 - 4.0 钩子函数和装饰器路由实现 session-cookie 请求上下文
钩子函数和装饰器路由实现 before_request 每次请求都会触发 before_first_requrest 第一次请求前触发 after_request 请求后触发 并返回参数 tear ...
- python---基础知识回顾(二)(闭包函数和装饰器)
一.闭包函数: 闭包函数: 1.在一个外函数中定义了一个内函数 2.内函数里运用了外函数的临时变量,而不是全局变量 3.并且外函数的返回值是内函数的引用.(函数名,内存块地址,函数名指针..) 正确形 ...
- Fastify 系列教程一(路由和日志)
介绍 Fastify是一个高度专注于以最少开销和强大的插件架构,为开发人员提供最佳体验的Web框架. 它受到了 Hapi 和 Express 的启发,是目前最快的 Node 框架之一. Fastify ...
- Fastify 系列教程三 (验证、序列化和生命周期)
Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) 验证 Fast ...
- Fastify 系列教程四 (求对象、响应对象和插件)
Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) Fastify ...
- Fastify 系列教程一 (路由和日志)
Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) Fastify ...
- CRL快速开发框架系列教程二(基于Lambda表达式查询)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- NGUI系列教程二
接下来我们创建一个Label,NGUI->Open the Widget Wizard,打开widgetTool对话框,在Template中选择Label,确定AddTo右侧选项为panel,点 ...
随机推荐
- day 44 django 学习入门
前情提要: 终于学到了Django ...古川小姐姐好流b .....7天学完.....脑壳疼..为了出了这个小火箭.. 详细参考官网. https://www.django.cn/ 中文网站 一: ...
- java API连接虚拟机上的hbase
今天用本地的eclipse连接虚拟机上的hbase数据库,代码如下: public static void main(String[] args) throws Exception{ Configur ...
- linux 中 iptables关于ping的问题
允许其他机器ping通防火墙 iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT iptables -A OUTPUT -p ic ...
- Mac 10.12连接iSCSI硬盘软件iSCSI Initiator X
Mac下的iSCSI协议苹果一直以来没有集成,而网络上流传的最好用支持iSCSI硬盘的软件是globalSAN,但是这个软件是收费的,当然有破解版,只不多不太好找,因为现在用iSCSI的用户已经很少了 ...
- python处理自然语言:1、调用LTP的API,2、使用pyltp
最近在学习处理自然语言处理,就发现LTP的(哈工大语言云),这个比我最先使用的jieba分词更好,词库更大,功能也更强大. 这里介绍两种方法:1.调用LTP的API,2.使用pyltp,这里的方法基于 ...
- Vue中使用children实现路由的嵌套
Vue中使用children实现路由的嵌套 相关Html: <!DOCTYPE html> <html lang="en"> <head> &l ...
- postman—环境切换和设置变量
postman提供了environment管理功能,想要在多个环境中测试,比如在测试环境.灰度环境.生产环境等,只需要用同样的接口,切换下环境即可,非常方便.具体步骤: 一.切换环境 1.点击界面右上 ...
- echart 饼图数据为0不显示或者太小显示其他的解决办法
饼图数据为0或者太小显示如下,不美观 解决办法: 为0的去掉,小于0.005的累加起来 方法 function getsData(_rowData){ var rowData=JSON.parse(J ...
- PMP备考指南之第二章:项目运作环境
本文已同步至 GitHub/Gitee/公众号,感兴趣的同学帮忙点波关注~ 第二章:项目运作环境 1. 事业环境因素.组织过程资产 事业环境因素 Enterprise Environmental Fa ...
- tomcat启动(Ⅶ)请求处理--Processor.process(SocketWrapper<S> socketWrapper)
tomcat启动(六)Catalina分析-StandardServer.start() 上一篇分析到:Http11NioProcessor.process(SocketWrapper<S> ...