express内部Layer结构
发布日期:2021-05-28 16:24:04 浏览次数:9 分类:技术文章

本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:typeorm之Entity Listeners
下一篇:express之layer.js

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年02月03日 20时00分57秒