php网站视频代码,进入公众号信阳云,怎么做网站建设赚钱,做网站怎么选关键词这段时间一直有在看Express框架的API#xff0c;最近刚看到Router#xff0c;以下是我认为需要注意的地方#xff1a; Router模块中有一个param方法#xff0c;刚开始看得有点模糊#xff0c;官网大概是这么描述的#xff1a; 1Map logic to route parameters.大概意思就…这段时间一直有在看Express框架的API最近刚看到Router以下是我认为需要注意的地方 Router模块中有一个param方法刚开始看得有点模糊官网大概是这么描述的 1 Map logic to route parameters. 大概意思就是路由参数的映射逻辑 这个可能一时半会也不明白其作用尤其是不知道get和param的执行顺序 再看看源码里面的介绍 1 2 3 Map the given param placeholder name(s) to the given callback. Parameter mapping is used to provide pre-conditions to routes which use normalized placeholders 这就清晰多了翻译过来就是说 在所给的参数和回调函数之间做一个映射作为使用标准化占位符的路由的前提条件。 下面给出一段具体代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 var express require(express); var app express(); var router express.Router(); router.count 0; router.get(/users/:user, function(req, res, next) { router.count ; console.log(router.count); }); router.param(user, function(req, res, next, id) { router.count ; res.send({count: router.count}); next(); }); app.use(router); app.listen(3000); 命令行下输入 node xxx.js 浏览器访问 http://localhost:3000/users/bsn 这时候命令行会输出2而浏览器会输出 {
count: 1
} 因此param会先于get执行转载于:https://www.cnblogs.com/chris-oil/p/4985542.html