Koa 中间件的执行
Node.js 中请求的处理讨论 Koa 中间件前,先看原生 Node.js 中是如何创建 server 和处理请求的。 node_server.js const http = require("http"); const PORT = 3000;const server = http.createServer((req, res) => {res.end("hello world!"); });server.listen(PORT); console.log(`server started at http://localhost:${PORT}`); Koa 中请求的处理Koa 也是通过上面的 koa/lib/application.js#L64 listen(...args) {debug('listen'); + const server = http.createServer(this.callback());return server.listen(...args);} Koa 中的 hello world: server.js const Koa = require("koa"); const app = new Koa();app.use(async ctx => {ctx.body = "Hello World"; });app.listen(3000); Koa 中,涉及到对请求返回处理都是通过中间件完成的,像上面为样,返回页面一个 Koa 中间件编写及使用Koa 中中间件即一个处理请求的方法,通过调用 koa/lib/application.js#L105 use(fn) {if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');if (isGeneratorFunction(fn)) {deprecate('Support for generators will be removed in v3. ' +'See the documentation for examples of how to convert old middleware ' +'https://github.com/koajs/koa/blob/master/docs/migration.md');fn = convert(fn);}debug('use %s', fn._name || fn.name || '-');this.middleware.push(fn);return this;} 通过上面的代码可看到,注册的中间件被压入 因为中间件中需要进行的操作是不可控的,完全有可能涉及异步操作,比如从远端获取数据或从数据库查询数据后返回到 比如实现计算一个请求耗时的中间件,以下分别是通过普通函数配合 Promise 以及使用 来自官方 README 中使用 Promise 实现中间件的示例代码 // Middleware normally takes two parameters (ctx, next), ctx is the context for one request, // next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.app.use((ctx, next) => {const start = Date.now();return next().then(() => {const ms = Date.now() - start;console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);}); }); 来自官方 README 中使用 app.use(async (ctx, next) => {const start = Date.now();await next();const ms = Date.now() - start;console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); }); 可以看到,一个中间件其签名是 server.js app.use(async (ctx, next) => {console.log(1);next(); }); app.use(async (ctx, next) => {console.log(2); }); app.use(async (ctx, next) => {console.log(3);ctx.body = "Hello, world!"; }); 执行后控制台输出: $ node server.js 1 2 访问页面也不会看到
|
转载于:https://www.cnblogs.com/Wayou/p/koa_middleware.html
Koa 中间件的执行相关推荐
- 深入理解 Koa 中间件之 “ 洋葱模型 ”
欢迎关注我的公众号『 前端我废了 』,查看更多文章!!! 前言 我们知道创建一个 Koa 应用主要分三步: const Koa = require('koa'); // 1. 创建一个 Koa 实例 ...
- 什么是koa中间件,他们的执行顺序是什么样的?
koa中间件 koa中,中间件分为应用级和路由级 //应用级 app.use(async (ctx, next) => { //应用级中间件 ,先执行中间件,再匹配路由console.log(& ...
- 初识洋葱模型,分析中间件执行过程,浅析koa中间件源码
前言 作为洋葱模型的第一篇文章,这里仅介绍了一些入门级知识,比如 了解洋葱模型执行顺序 分析部分 koa 中间件的源码来加深对中间件的认识 为第二篇文章:分析洋葱模型实现原理,在自己项目中接入洋葱模型 ...
- egg koa 中间件执行原理,洋葱模型原理
1.官网例子 const Koa = require('koa'); const app = new Koa();// logger app.use(async (ctx, next) => { ...
- koa 接口返回数据_一文搞定 Koa 中间件实现原理
Koa是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小.更富有表现力.更健壮的基石. 通过利用 async 函数, Koa ...
- web前端技术分享:koa中间件是如何实现的?
在前端开发过程中我们可能会使用到koa中间件,但很多同学却不知道它是如何实现的,下面小千就来给大家介绍一下这个koa中间件(洋葱模型). 一.问题分析 async await是promise的语法糖, ...
- generator探幽(1)--koa中间件机制浅析
本系列旨在通过对co,koa等库源码的研究,进而理解generator在异步编程中的重大作用(ps:所有代码请在node --harmony或者iojs环境中运行) koa中间件的形式 相信用过koa ...
- 【nodejs原理源码赏析(2)】KOA中间件的基本运作原理
[摘要] KOA中间件的基本运作原理 示例代码托管在:http://www.github.com/dashnowords/blogs 在中间件系统的实现上,KOA中间件通过async/await来在不 ...
- koa 中间件洋葱模型源码分析
中间件基本使用 app.use(async(ctx,next)=>{ctx.state={username:'jeff'};await next();... })app.use(async(ct ...
最新文章
- oracle 游标小例
- Web UI回归测试 -- BackstopJS 入门
- Solr+Hbase多条件查(优劣互补)
- 为什么要选择Hibernate
- 追了多年的开发框架,你还认识指针吗?
- 英特尔cpu发布时间表_英特尔10nm芯片开始大规模出货,先进制程时间表浮出水面...
- mysql slow log 分析工具_mysql slow log分析工具的比较
- 计算机技术在机械设计中的应用,计算机技术在机械设计制造和自动化中的应用(原稿)...
- 为什么说电商创业的机会在变少?
- php实现从尾到头打印列表
- 泛型list集合类转换成DataTable、datatable转list
- linux播放csf文件
- 洛谷P5369 [PKUSC2018]最大前缀和 [DP]
- 服务器修改硬盘顺序,服务器硬盘阵列硬盘顺序
- android7工程自测模式,工程模式测试
- 什么是TDK?什么是网站的TDK?扫(myself的)盲
- ArcGIS的栅格数据空间分析——栅格插值(1)
- 数据资产价值评估与定价:研究综述和展望
- jsp servlet mysql项目_JSP+Servlet+JDBC+mysql实现的个人日记本系统
- 欧飞信科技ELEXCON 2022深圳国际电子展精彩回顾
热门文章
- 计量经济学实验报告计算机,计量经济学-实验报告.doc
- 安卓 camera 调用流程_安卓如何做出微信那样的界面仿微信“我”的界面1/5
- 1+X web中级 Laravel学习笔记——blade模版
- Node 中的开发环境与生产环境 和 使用Morgan打印请求信息
- 剑指Offer - 面试题50. 第一个只出现一次的字符(unordered_map)
- Linux单用户能做什么,Linux单用户模式详解 及应用场景
- 利用python批量修改文件名称
- python数据分析开发环境_在MAC上搭建python数据分析开发环境
- python网络编程内容_Python网络编程
- 消息中间件系列(五):MQ消息队列的12点核心原理总结