原创转载请注明出处:http://agilestyle.iteye.com/blog/2339754
Middleware
Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.
Middleware functions can perform the following tasks:
- Execute any code.
- Make changes to the request and the response objects.
- End the request-response cycle.
- Call the next middleware in the stack.
If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.
The following figure shows the elements of a middleware function call:
The middleware stack
Web Server通常会监听request,解析request,并且发送response。Node运行环境首先会获取这些request并且将它们从原生字节转换为你可以处理的两个JavaScript对象:request(req)和response(res)。当仅仅使用Node.js,流程图如下所示。
这两个对象将会被送到一个你自己定义的JavaScript的Function中。你会自己解析req来确认用户想要什么,并且巧妙的处理res来准备你的响应。
过了一会儿,你将通过调用res.end来完成响应的编写。这个res.end告诉Node响应已经完成,可以准备发送了。Node运行环境首先将查看你对响应对象做了什么,然后将它转换成另外一束字节,最终发送给在网络上请求的用户。
在Node中,这两个对象仅仅是通过一个Function传递的。但是在Express中,这些对象是通过一系列Function传递的,称为middleware stack。Express将从stack中的第一个Function开始并且沿着Stack向下顺序执行,如下图所示。
每一个在Stack中的函数接收3个参数。第一个两个是req和res,它们通过Node提供;每个函数的第三个参数本身就是一个函数,按照惯例称为next。当next被调用,Express将运行Stack中的下一个函数,如下图所示。
最终,在Stack中的一个函数必须调用res.end,表示结束请求(在Express中,也可以调用其他的方法,比如:res.send或者res.sendFile,但是这些方法内部是调用res.end的)。你可以调用在middleware stack中的任意一个函数的res.end,但是只能调用一次,否则将报错。
Reference
http://expressjs.com/en/guide/writing-middleware.html
Manning.Express.in.Action.Node.applications.with.Express.and.its.companion.tools.2016
相关推荐
After you have a solid grasp on Node and Mongo, the book will move on to the Express web server. We’ll address the basics of Express applications via topics such as routes and middleware. Building ...
Configuring Full-Stack with Node.js, Express.js, MongoDB, Mongoose, Falcor, and Redux Full-Stack Login and Registration for Our Publishing App Server-Side Rendering Advanced Redux and Falcor on the ...
This is due to the huge developer community and it being used in every layer of an application technology stack. This book will teach you how to develop JavaScript applications with simple to use, ...
Mean-stack-Angular-7-crud-app-example的例子循序渐进的MEAN Stack教程,您将学习从零开始使用Bootstrap 4,MongoDB,NodeJS和Express JS构建Angular 7 CRUD应用程序。 该项目是使用Angular CLI版本7.2.3构建的。 ...
(Node.js) Connectr是之上的一层,允许在构建堆栈之后插入/删除中间件。 当您无权访问设置Connect堆栈的代码(例如第三方模块)时,此功能...// Label middleware which is at a specific position on the stack//
webApp_MESI_FullStack 前端:vue.js middleWare:express(axios)后端:AWS(Linux + Ubuntu)+ nodejs
React MERN Stack CRUD应用程序教程-使用React,Node,Express,mongoDB创建前端和后端。 并学习使用React Axios使用REST API。 该项目是通过。 分步教程 可用脚本 在项目目录中,可以运行: npm start 在开发模式...
// and run the middleware stack: app.run({val: 'hello'}, {}) .then(output => { // output.prop === 2 });示例代码:const http = require('http'); const middl = require('middl...
ShoppingCart-Vue.js + Node.js + Express + MongoDB 使用Vue.js开发ShoppingCart(电子商务)应用程序 现场演示: 该项目是使用版本3.x生成的 功能性 使用Passport.js的用户注册和身份验证(电子邮件/密码| ...
从 Linux® 2.6内核及整个 software stack (包括 kernel、middleware 以及各种用户态 utility)如何添加对这些创新的节能技术的支持这一角度,为读者介绍 Linux 操作系统近几年来在电源管理方面所取得的长足进步...
从头开始构建MERN(MongoDB | Express | ReactJS | Nodejs)堆栈应用话题影片日期设置Reactjs和Nodejs结构2019年11月18日在同一终端中运行ReactJS和Nodejs-同时2019年11月18日Git工作流程2019年11月18日使用Mongoose...
Angular8MeanstackAngularMaterial 有关的分步文章启动MEAN Stack应用启动Angular应用并安装所需的依赖项git clone 从项目的根目录运行npm install 运行ng serve --open以开始角度开始后端运行cd backend 运行npm ...
这是现代网络的框架; 小巧,光滑,优雅,快速。 我们花了太多时间试图解决我们在其他框架上遇到的问题之后,才构建了Rayo 。 我们需要一种几乎可以直接... Rayo并非Express的替代产品,因此API相似,受启发但不完全相
均值欺诈 基本的MEAN项目。 通过Angular FrontEnd客户端使用在Node.js上运行的Express API对MongoDB执行CRUD操作 :beaming_face_with_smiling_eyes:
:check_mark: 没有Express.js :check_mark: 无服务器就绪 :check_mark: API路由 :check_mark: 中间件 :locked: 验证 会议 注册/登录/注销 :woman: :man: 轮廓 个人资料图片,姓名,个人简历,电子邮件 编辑...
Lad的Web服务器目录安装 : npm install @ladjs/web : yarn add @ladjs/web用法有关最新的用法示例,请参见 。贡献者名称网站尼克·鲍执照:copyright: