如何优化Express.js路由? [英] How to optimize an Express.js route?

查看:136
本文介绍了如何优化Express.js路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有以下几个页面的保留区域:

  / dashboard 
/ dashboard /个人资料
/ dashboard / user
/ dashboard / view

这是一个简单的用户控制面板。目前我有四条路线:

  app.all('/ dashboard',function(req,res,next){ / * Code * /}); 
app.all('/ dashboard / profile',function(req,res,next){/ * Code * /});
app.all('/ dashboard / user',function(req,res,next){/ * Code * /});
app.all('/ dashboard / view',function(req,res,next){/ * Code * /});

我想优化它,因为在上述每个路线中,我必须在开始:

 授权(req,function(auth){
if(!auth)return next(errors.fire (403));
/ *路由代码* /
});

此功能检查用户是否已记录,因此我需要在每个保留页面上调用它。 / p>

我会这样做:

  app.all('仪表板/ *',功能(req,res,next){

authorized(req,function(auth){
if(!auth)return next(errors.fire(403)) ;
res.render(something,{})
});

});

在res.render调用中的某事必须是我需要打开的视图(页面)。



我想将其称为 ONE 时间,以删除冗余代码。



这可能是面板的家(如果用户想要/仪表板)或页面(如果用户想要一个页面/仪表板,如/ dashboard / profile)在最后一种情况下,我需要渲染配置文件视图。 p>

(我必须在将视图传递给render()之前进行检查,因为如果有人尝试/ dashboard / blablablabla应该是一个问题。)



谢谢

解决方案

您可以将该功能作为路由中间件传递到每个路由, http://expressjs.com/guide.html#route-middleware 了解更多信息。这个想法是这样的:

 函数mustBeAuthorized(req,res,next){
/ *需要你的代码授权用户* /
}

然后在每个路由中:

  app.all('/ dashboard',mustBeAuthorized,function(req,res,next){/ * Code * /}); 

或者如果您的逻辑依赖于每个路由的某个角色,您可以使路由中间件像这样:

 函数mustBeAuthorizedFor(role){
return function(req,res,next){
/ *您的代码需要授权用户使用该ROLE * /
};
}

然后立即致电:

  app.all('/ dashboard',mustBeAuthorizedFor('dashboard'),function(req,res,next){/ * Code * /}); 


I'm developing a reserved area that has the follow few pages:

/dashboard
/dashboard/profile
/dashboard/user
/dashboard/view

that's a simple user control panel. At the moment i have four routes:

app.all('/dashboard', function(req, res, next) { /* Code */ }); 
app.all('/dashboard/profile', function(req, res, next) { /* Code */ }); 
app.all('/dashboard/user', function(req, res, next) { /* Code */ }); 
app.all('/dashboard/view', function(req, res, next) { /* Code */ }); 

I would like to optimize it because in each of the above routes i have to call this function at the beginning:

authorized(req, function(auth){
   if (!auth) return next(errors.fire(403));
   /* route code */
});

This function checks if the user is logged, so i need to call it on every reserved page.

I would do something like:

app.all('/dashboard/*', function(req, res, next) { 

    authorized(req, function(auth){
       if (!auth) return next(errors.fire(403));           
       res.render(something, {})     
    });

});

the something inside the res.render call has to be the view (page) I need to open.

I want to call it ONE time, to remove redundant code.

That could be the home of the panel (if the user wants /dashboard) or the page (if the user wants a page inside /dashboard like /dashboard/profile) in the last case i need to render 'profile' view.

(I have to do a check before pass the view to render(), because if someone try /dashboard/blablablabla it should be a problem.)

Thank you

解决方案

You can pass that function to each route as a route middleware, check http://expressjs.com/guide.html#route-middleware for more info. The idea would be this:

function mustBeAuthorized(req, res, next){
  /* Your code needed to authorize a user */
}

And then in each route:

app.all('/dashboard', mustBeAuthorized, function(req, res, next) { /* Code */ }); 

Or if your logic depends on a certain role for each route, you can make the route middleware like this:

function mustBeAuthorizedFor(role){
  return function(req, res, next){
     /* Your code needed to authorize a user with that ROLE */
  };
}

And then call it right away:

app.all('/dashboard', mustBeAuthorizedFor('dashboard'), function(req, res, next) { /* Code */ }); 

这篇关于如何优化Express.js路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆