express内部Layer结构
发布日期:2021-05-28 16:24:04
浏览次数:27
分类:技术文章
本文共 5377 字,大约阅读时间需要 17 分钟。
app.jsvar app = express();// view engine setupapp.set('views', path.join(__dirname, 'views'));app.set('view engine', 'jade');app.use(logger('dev'));app.use(bodyParser.json());app.use(bodyParser.urlencoded({ extended: false }));app.use(cookieParser());app.use(express.static(path.join(__dirname, 'public')));app.use('/', index);app.use('/users', users);app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err);});app.use(function(err, req, res, next) { res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; res.status(err.status || 500); res.render('error');});console.dir(app._router.stack)可以看到app实例上挂载了以下元素:
1.logger('dev'):日志中间件
2.bodyParser.json():json请求解析中间件
3.bodyParser.urlencoded({extended:false}):urlencoded请求解析中间件
4.cookieParser():cookie解析中间件
5.express.static():静态资源中间件,好像4.0中去掉了
6.index:为一个router对象
7.users:为一个router对象
最后就是两个匿名中间件函数,挂载在/上,最后输出结果为
[ Layer { handle: [Function: query], name: 'query', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: [Function: expressInit], name: 'expressInit', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: [Function: logger], name: 'logger', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: [Function: jsonParser], name: 'jsonParser', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: [Function: urlencodedParser], name: 'urlencodedParser', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: [Function: cookieParser], name: 'cookieParser', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: [Function: serveStatic], name: 'serveStatic', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: { [Function: router] params: {}, _params: [], caseSensitive: undefined, mergeParams: undefined, strict: undefined, stack: [Array] }, name: 'router', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: { [Function: router] params: {}, _params: [], caseSensitive: undefined, mergeParams: undefined, strict: undefined, stack: [Array] }, name: 'router', params: undefined, path: undefined, keys: [], regexp: { /^\/users\/?(?=\/|$)/i fast_star: false, fast_slash: false }, route: undefined }, Layer { handle: [Function], name: '', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined }, Layer { handle: [Function], name: ' ', params: undefined, path: undefined, keys: [], regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true }, route: undefined } ]
可以看到:
1.前面多出了两个中间件:query、expressInit,它们是在lazyrouter方法中设置在根router中的
2.普通中间件层的handle为[Function: query],其中query为中间件方法名,而挂载router对象的方法名为router,匿名中间件方法名为<anonymous>
下面是两个router对象:
index.js/* GET home page. */router.get('/', function(req, res, next) { res.render('index', { title: 'Express' });});module.exports = router;
users.js/* GET users listing. */router.get('/', function(req, res, next) { res.send('respond with a resource');});module.exports = router;
打印两个router对象的stack堆
console.dir(app._router.stack[7].handle.stack)console.dir(app._router.stack[8].handle.stack)
输出为:
[ Layer { handle: [Function: bound dispatch], name: 'bound dispatch', params: undefined, path: undefined, keys: [], regexp: { /^\/?$/i fast_star: false, fast_slash: false }, route: Route { path: '/', stack: [Array], methods: [Object] } } ][ Layer { handle: [Function: bound dispatch], name: 'bound dispatch', params: undefined, path: undefined, keys: [], regexp: { /^\/?$/i fast_star: false, fast_slash: false }, route: Route { path: '/', stack: [Array], methods: [Object] } } ]
可以看到:
1.router对象上使用route方法建立的layer的handle为Route对象的dispatch方法,bound意味着这是一个使用bind绑定了this的方法
2.router的路由层有route属性,为一个Route对象,Route对象也有一个stack堆
下面打印Route对象的stack堆
console.dir(app._router.stack[7].handle.stack[0].route.stack)console.dir(app._router.stack[8].handle.stack[0].route.stack)
输出为:
[ Layer { handle: [Function], name: '', params: undefined, path: undefined, keys: [], regexp: { /^\/?$/i fast_star: false, fast_slash: false }, method: 'get' } ][ Layer { handle: [Function], name: ' ', params: undefined, path: undefined, keys: [], regexp: { /^\/?$/i fast_star: false, fast_slash: false }, method: 'get' } ]
1.这里Layer出现了method属性,说明用来处理相应的http请求
转载地址:https://blog.csdn.net/qq_27868061/article/details/79275758 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年09月24日 18时17分51秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
数据库Sharding的基本思想和切分策略
2019-06-07
数学图形(2.7)sphere sine wave
2019-06-07
数学图形之鞍面
2019-06-07
seajs +gruntjs 合作开发
2019-06-07
[5]windows内核情景分析---进程线程
2019-06-07
Fluuter常遇到的问题
2019-06-07
计算机基础小结
2019-06-07
递归算法以及求层级
2019-06-07
UISegmentedControl的详细使用
2019-06-07
存储过程优点&缺点
2019-06-07
Java基础知识总结
2019-06-07
读书笔记——高效能人士的七个习惯2(四类优先级)
2019-06-07
思维_逆向的魅力_20131229
2019-06-07
Trap 冷启动与热启动告警
2019-06-07
jquery操作字符串常用方法总结及工作代码
2019-06-07
nova availability zone
2019-06-07
Flask入门之Pycharm写Hello Word
2019-06-07