全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
express 框架之 路由与中间件1. 什么是router路径,什么是middleware?我们输入 来访问百度的主页,浏览器会自动转换为 :80/(省略一些参数)。 http:/代表我们同服务器连接使用的是http协议, 代表的是服务器的主机地址,会被我们的pc通过DNS解析为IP地址。80是默认的应用层端口。/ 即为我们访问的服务器()的路径,服务器要对我们访问的这个路径做出响应,采取一定的动作。我们可以把这一过程看做一个路由。访问的路径/即为router的路径,服务器采取的动作即为middleware,即为一个个特殊的函数。2. router路径/test: 路径为 //test?name=1&number=2: 路径同样为/test, ?后面会被服务器理解传给路径的参数。3. MiddlewareAn Express application is essentially a stack of middleware which are executed serially.(express应用其实就是由一系列顺序执行的Middleware组成。)A middleware is a function with access to the request object (req), the response object (res), and the next middleware in line in the request-response cycle of an Express application. It is commonly denoted by a variable named next. Each middleware has the capacity to execute any code, make changes to the request and the reponse object, end the request-response cycle, and call the next middleware in the stack. Since middleware are execute serially, their order of inclusion is important.(中间件其实就是一个访问express应用串入的req,res,nex参数的函数,这个函数可以访问任何通过req,res传入的资源。)If the current middleware is not ending the request-response cycle, it is important to call next() to pass on the control to the next middleware, else the request will be left hanging.(如果当前中间件没有完成对网页的res响应 ,还可以通过next把router 留给下一个middleware继续执行)With an optional mount path, middleware can be loaded at the application level or at the router level. Also, a series of middleware functions can be loaded together, creating a sub-stack of middleware system at a mount point.路由的产生是通过HTTP的各种方法(GET, POST)产生的,Middleware可以跟router路径跟特定的HTTP方法绑定,也可以跟所有的方法绑定。3.1 通过express应用的use(all),把Middleware同router路径上的所有HTTP方法绑定:1 app.use(function (req, res, next) 2 console.log(Time: %d, Date.now();3 next();4 )3.2通过express应用的http.verb,把Middleware同router路径上的特定的HTTP方法绑定:1 app.get(/, function(req, res)2 res.send(hello world);3 );4 5 6 app.post(/, function(req, res)7 res.send(hello world);8 );4. Express的Router对象当express实例的路由越来越多的时候,最好把路由分类独立出去,express的实例(app) 能更好的处理其他逻辑流程。Express的Router对象是一个简化的 app实例,只具有路由相关的功能,包括use, http verbs等等。最后这个Router再通过app的use挂载到app的相关路径下。 1 var express = require(express); 2 var app = express(); 3 var router = express.Router(); 4 5 / simple logger for this routers requests 6 / all requests to this router will first hit this middleware 7 router.use(function(req, res, next) 8 console.log(%s %s %s, req.method, req.url, req.path); 9 next();10 );11 12 / this will only be invoked if the path ends in /bar13 router.use(/bar, function(req, res, next) 14 / . maybe some additional /bar logging .15 next();16 );17 18 / always invoked19 router.use(function(req, res, next) 20 res.send(Hello World);21 );22 23 app.use(/foo, router);24 25 app.listen(3000);router的路由必须通过app.use和app.verbs 挂载到app上才能被响应。所以上述代码,只有在app捕捉到 /foo路径上的路由时,才能router中定义的路由,虽然router中有针对 / 的路由,但是被app中的路由给覆盖了。附:app.verbs和app.use的路由路径区别:先看一段测试代码: 1 var express = require(express); 2 3 var app = express(); 4 var router = express.Router(); 5 6 app.get(/, function(req, res) 7 console.log(test1); 8 ); 9 10 app.use(/, function(req, res)11 console.log(test2);12 );13 14 router.get(/, function(req, res)15 console.log(test3);16 );17 18 app.listen(4000);输入url:localhost:4000输出结果:test1输入url: localhost:4000/hello输出结果:test2结论:app.get挂载/的路由只响应跟/精确匹配的GET请求。 而app.use挂载的/的路由响应所有以/ 为起始路由的路由,且不限制HTTP访问的方法。以下说明:Mounting a middleware at a path will cause the middleware function to be executed wheneverthe base ofthe requested path matches the path.1 app.use(path, function., function)2 Mount the middleware function(s) at the pa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年风湿病练习题库【B卷】附答案详解
- 2026年核技术利用辐射安全与防护考核测试卷含答案详解(夺分金卷)
- 2026年大学电机试与控制期末练习题含完整答案详解【历年真题】
- 2025年康复科医师康复评估量表应用考试试题及答案解析
- 2026福建泉州物产集团有限公司及权属企业招聘劳务派遣制人员2人笔试历年参考题库附带答案详解
- 2026浙江绍兴市镜湖开发集团有限公司下属国有企业招聘项目制员工总及人员笔试历年参考题库附带答案详解
- 2026河南驻马店市强力建材有限公司招聘笔试笔试历年参考题库附带答案详解
- 2026江西吉安市遂川县城控人力资源管理有限公司招聘拟入闱投档分数线及安排笔试历年参考题库附带答案详解
- 2026中国兵器工业第二三研究所招聘笔试历年参考题库附带答案详解
- 2025陕西延长石油物流集团有限公司包装制品分公司人员招聘32人笔试历年参考题库附带答案详解
- 湘教版小学音乐二年级下册全册教案
- 初升高选拔考试数学试卷
- 广东能源集团校园招聘笔试题库
- JJF 2019-2022 液体恒温试验设备温度性能测试规范
- CJT340-2016 绿化种植土壤
- 唐诗宋词人文解读 知到智慧树网课答案
- 文本信纸(A4横条直接打印版)模板
- 森林灾害防护知识讲座
- 国家义务教育质量监测科学四年级创新作业测试卷附答案
- 米糠的综合利用教学
- 造船企业管理 造船成本组成
评论
0/150
提交评论