Fastify 系列教程:

中间件

Fastify 提供了与 ExpressRestify 中间件兼容的异步中间件引擎

所以,Fastify 可以使用 Express 中间件

注意:Fastify 中没有错误处理中间件(err, req, res, next)。如果需要处理中间件中的错误时,只需要调用 next(new Error('error message')) 即可,Fastify 会为你关闭请求并发送错误响应。

示例:

访问静态资源

配置 serve-static 访问静态资源。

目录结构:

  1. public
  2. |-css
  3. |-js
  4. |-jquery.js
  5. |-images
  6. views
  7. app.js

app.js:

  1. const serveStatic = require('serve-static')
  2. const path = require('path')
  3. fastify.use('/public', serveStatic(path.resolve(__dirname, 'public')))

此时,访问 localhost:3030/public/js/jquery.js 可以正确获得静态资源文件。

注意:中间件的请求和响应参数是原生 Nodejs Http 的 reqres,而路由中的 requestreply 是经过 Fastify 封装的,两者并不一样:

  1. const fastify = require('fastify')()
  2. fastify.use(function(req, res, next){
  3. req.hello = "req.hello"
  4. res.hello = "res.hello"
  5. next()
  6. })
  7. fastify.use(function(req, res, next){
  8. console.log('middle:', req.hello)
  9. console.log('middle:', res.hello)
  10. next()
  11. })
  12. fastify.get('/', function (request, reply) {
  13. console.log('route:', request.hello)
  14. console.log('route:', reply.hello)
  15. reply.send({ hello: 'world' })
  16. })
  17. /*
  18. middle: req.hello
  19. middle: res.hello
  20. route: undefined
  21. route: undefined
  22. */

钩子函数

Fastify 中共有四个应用级钩子和一个路由级钩子。

四个应用级钩子:

  • onRequest(req, res, next)
  • preHandler(request, reply, next)
  • onResponse(res, next)
  • onClose(instance, done)

一个路由级钩子:

  • beforeHandler(request, reply, done)

注意 reqresrequestreply 的区别。

参数 描述
req Node.js Http 请求对象
res Node.js Http 响应对象
request Fastify Request 接口
reply Fastify Reply 接口
next 继续下一个 生命周期 任务

使用 addHook 方法添加钩子函数:

  1. fastify.addHook('onRequest', (req, res, next) => {
  2. // some code
  3. next()
  4. })
  5. fastify.addHook('preHandler', (request, reply, next) => {
  6. // some code
  7. next()
  8. })
  9. fastify.addHook('onResponse', (res, next) => {
  10. // some code
  11. next()
  12. })

如果在执行钩子函数时遇到错误,只需将其传递给 next(),并且 Fastify 将自动关闭请求并向用户发送相应的错误代码。

  1. fastify.addHook('onRequest', (req, res, next) => {
  2. next(new Error('some error'))
  3. })

如果你希望传递一个自定义状态码,可以使用reply.code():

  1. fastify.addHook('preHandler', (request, reply, next) => {
  2. reply.code(500)
  3. next(new Error('some error'))
  4. })

onClose

onClose 是唯一不在生命周期中的钩子,当调用 fastify.close() 来停止服务器时,会触发此钩子,第一个参数是 Fastify 实例,第二个用来完成回调。

  1. const fastify = require('fastify')()
  2. fastify.get('/close', function(request, reply){
  3. reply.type('text/html').send('<h1>Close Server</h1>')
  4. fastify.close()
  5. })
  6. fastify.onClose(function(instance, done){
  7. console.log('close db connection')
  8. done()
  9. })

访问 /close 时页面会显示 Close Server,并且控制台会输出:

  1. [Running] node "/Users/lavyun/Code/node/learn-fastify/app.js"
  2. close db connection
  3. [Done] exited with code=0 in 8.524 seconds

在关闭数据库链连接之后,app.js 也被 exit 了。

preHandler 和 beforeHandler

preHandler 的受众对象是所有的路由,而 beforeHandler 的受众对象是某个特定的路由,另外,beforeHandler 总是在 preHandler 之后执行。

  1. fastify.addHook('preHandler', (request, reply, done) => {
  2. console.log('preHandler')
  3. done()
  4. })
  5. fastify.get('/', {
  6. beforeHandler: function (request, reply, done) {
  7. console.log('beforeHandler')
  8. done()
  9. }
  10. }, function (request, reply) {
  11. reply.send({ hello: 'world' })
  12. })
  13. // preHandler
  14. // beforeHandler

beforeHandler 也可以是一个函数数组:

  1. fastify.addHook('preHandler', (request, reply, done) => {
  2. console.log('preHandler')
  3. done()
  4. })
  5. const firstBeforeHandler = (request, reply, done) => {
  6. console.log('first beforeHandler')
  7. done()
  8. }
  9. const secondBeforeHandler = (request, reply, done) => {
  10. console.log('second beforeHandler')
  11. done()
  12. }
  13. fastify.get('/', {
  14. beforeHandler: [firstBeforeHandler, secondBeforeHandler]
  15. }, function (request, reply) {
  16. reply.send({ hello: 'world' })
  17. })
  18. // preHandler
  19. // first beforeHandler
  20. // second beforeHandler

装饰器

如果想为 Fastify 实例添加功能,可以使用 decorate 方法。

decorate 允许向 Fastify 实例添加新属性。可以是一个值、一个函数,也可以是一个对象或一个字符串等。

使用方法

decorate

只需要调用 decorate 函数,并且传入新属性的键和值即可。

  1. fastify.decorate('utility', () => {
  2. // something very useful
  3. })

也可以定义其他类型的实例:

  1. fastify.decorate('conf', {
  2. db: 'some.db',
  3. port: 3000
  4. })

一旦定义了这个实例,可以通过传入的参数名称来得到该值:

  1. fastify.utility()
  2. console.log(fastify.conf.db)

装饰器不可以重新被覆盖,如果要定义一个已经存在的装饰器,decorate 将会抛出异常。

  1. fastify.decorate("d1", 'd1')
  2. fastify.decorate("d1", "d2") // Error

decorateReply

decorateReply 可以为 Reply 对象添加新属性。

  1. fastify.decorateReply('utility', function () {
  2. // something very useful
  3. })

decorateRequest

decorateRequest 可以为 Request 对象添加新属性。

  1. fastify.decorateRequest('utility', function () {
  2. // something very useful
  3. })

extendServerError

如果要扩展 服务器错误,可以使用此 API,必须传入一个返回值为对象的函数,该函数将接收原始的 Error 对象,并返回新Error 对象来扩展服务器错误。

  1. fastify.extendServerError((err) => {
  2. return {
  3. timestamp: new Date()
  4. }
  5. })
  6. /*
  7. 最终的错误对象格式:
  8. {
  9. error: String
  10. message: String
  11. statusCode: Number
  12. timestamp: Date
  13. }
  14. */

依赖

如果一个装饰器依赖于另一个装饰器,可以将其他装饰器声明为依赖。只需要添加一个字符串数组(表示所依赖的装饰器的名称)作为第三个参数即可:

  1. fastify.decorate('utility', fn, ['greet', 'log'])

如果不满足依赖关系,那么 decorate 会抛出一个异常,但是不用担心:依赖关系检查会在服务器启动之前执行,所以在运行时不会发生错误。

hasDecorator

可以使用 hasDecorator 检查装饰器是否存在:

  1. fastify.hasDecorator('utility')

Fastify 的更多使用将在接下来的博客中说明。

Tips:

访问 https://lavyun.gitbooks.io/fastify/content/ 查看我翻译的 Fastify 中文文档。

访问 lavyun.cn 查看我的个人动态。

Fastify 系列教程二 (中间件、钩子函数和装饰器)的更多相关文章

  1. Fastify 系列教程二 (中间件、钩子函数和装饰器)

    Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) 中间件 Fastify 提供了与 Express 和 Restify ...

  2. 【Flask】 python学习第一章 - 4.0 钩子函数和装饰器路由实现 session-cookie 请求上下文

    钩子函数和装饰器路由实现 before_request 每次请求都会触发 before_first_requrest  第一次请求前触发 after_request  请求后触发 并返回参数 tear ...

  3. python---基础知识回顾(二)(闭包函数和装饰器)

    一.闭包函数: 闭包函数: 1.在一个外函数中定义了一个内函数 2.内函数里运用了外函数的临时变量,而不是全局变量 3.并且外函数的返回值是内函数的引用.(函数名,内存块地址,函数名指针..) 正确形 ...

  4. Fastify 系列教程一(路由和日志)

    介绍 Fastify是一个高度专注于以最少开销和强大的插件架构,为开发人员提供最佳体验的Web框架. 它受到了 Hapi 和 Express 的启发,是目前最快的 Node 框架之一. Fastify ...

  5. Fastify 系列教程三 (验证、序列化和生命周期)

    Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) 验证 Fast ...

  6. Fastify 系列教程四 (求对象、响应对象和插件)

    Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) Fastify ...

  7. Fastify 系列教程一 (路由和日志)

    Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) Fastify ...

  8. CRL快速开发框架系列教程二(基于Lambda表达式查询)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  9. NGUI系列教程二

    接下来我们创建一个Label,NGUI->Open the Widget Wizard,打开widgetTool对话框,在Template中选择Label,确定AddTo右侧选项为panel,点 ...

随机推荐

  1. SVN版本服务器的搭建和远程控制

    版本服务器是用SVN server(这个东西是放到版本机服务器上的)  版本管理工具是用小乌龟(tortoiseSVN,这个是在各个机器上使用) 1,昨天下载了SVN server 按照网上教程搭建好 ...

  2. Storm的并行度

    在Storm集群中,运行Topolopy的实体有三个:工作进程,executor(线程),task(任务),下图可以形象的说明他们之间的关系. 工作进程 Storm集群中的一台机器会为一个或则多个To ...

  3. 微信小程序redirect 到tab不刷新

    // 更新2018/11/20:现在小程序的页面栈长度为10 更正 2018/11/20: 经过一段时间的实践,我发现以前方法存在很多问题,比如 getCurrentPages 方法并不在官方的 AP ...

  4. [转][SQL]如何实现存储过程中动态加入条件---没想到语句可以这么巧妙

    在存储过程过程中,如果要实现Select查询的where子句动态查询,可以用exec ( "select .... where" +@whereStr)这样的方式.但这样的话,感觉 ...

  5. 直接访问实例变量 VS 通过点语法访问实例变量

    直接访问实例变量,不会经过 OC 的方法派发机制,速度比较块.会直接访问对象的实例变量对应的内存. 直接访问实例变量,不会调用"设置方法".绕过了相关属性对应的"内存管理 ...

  6. postgresql子查询优化(提升子查询)

    问题背景 在开发项目过程中,客户要求使用gbase8s数据库(基于informix),简单的分页页面响应很慢.排查发现分页sql是先查询出数据在外面套一层后再取多少条,如果去掉嵌套的一层,直接获取则很 ...

  7. php5数组与php7数组区别

    http://ju.outofmemory.cn/entry/197064 http://www.fzb.me/2015-9-16-php7-implementation-hashtable.html ...

  8. 配置豆瓣镜像作为python 库的下载源

    配置豆瓣镜像作为python 库的下载源 Windows 下如下配置:

  9. hive 0.12.0版本 问题及注意事项

    下载地址: http://archive.cloudera.com/cdh5/cdh/5/hive-0.12.0-cdh5.1.5.tar.gz 用远程mysql作为元数据存储 创建数据库,设置字符集 ...

  10. 我与GitHub的第一次——自制音乐文件修改器

    背景: 随机播放,所有的音乐播放器里面现在几乎都有这个功能吧.但是有没有发现,自己的播放器在选择随机播放的时候,经常会听到重复顺序的歌曲呢?反正我是有这样的感觉,无耐自己平时下的歌曲都是“歌手名—歌曲 ...