1. 什么是router路径,什么是middleware?express
咱们输入www.baidu.com 来访问百度的主页,浏览器会自动转换为 http://www.baidu.com:80/(省略一些参数)。 http://表明咱们同服务器链接使用的是http协议,www.baidu.com 表明的是服务器的主机地址,会被咱们的pc经过DNS解析为IP地址。80是默认的应用层端口。/ 即为咱们访问的服务器(www.baidu.com)的路径,服务器要对咱们访问的这个路径作出响应,采起必定的动做。咱们能够把这一过程看作一个路由。
访问的路径‘/’即为router的路径,服务器采起的动做即为middleware,即为一个个特殊的函数。浏览器
2. router路径服务器
www.baidu.com/test: 路径为 /testapp
www.baidu.com/test?name=1&number=2: 路径一样为/test, ?后面会被服务器理解传给路径的参数。ide
3. Middleware 函数
An 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方法绑定,也能够跟全部的方法绑定。post
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方法绑定:ui
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对象this
当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 router's 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 /bar 13 router.use('/bar', function(req, res, next) { 14 // ... maybe some additional /bar logging ... 15 next(); 16 }); 17 18 // always invoked 19 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
1 app.use([path], [function...], function) 2 Mount the middleware function(s) at the path. If path is not specified, it defaults to "/". 3 4 Mounting a middleware at a path will cause the middleware function to be executed whenever the base of the requested path matches the path.