filter.js上的过滤器 [英] Filters on express.js

查看:146
本文介绍了filter.js上的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在过滤express.js之前做一个像rails这样的过滤器。
我有一个名为photo.js的文件,我把所有照片相关的路线放在那里。但是我需要将我的系统上未认证的用户重定向到登录页面。我想做一个beforeFilter,所以我不需要把这个逻辑在我所有的路线...

I want to do a filter like rails before filter on express.js. I have a file named photo.js where I'm putting all my photo related routes on there. But I need to redirect user that is not authenticated on my system to the login page. I want to do an beforeFilter so then I dont need to put that logic in all my routes...

谢谢

推荐答案

如果你想保留你的photo.js文件中的所有内容,我认为一个更好的方法是使用app.all并传递多个回调(它像路由中的中间件)建立进入应用路由。例如

If you want to keep everything in your photo.js file, I think a better approach is to use app.all and pass multiple callbacks (which work like middleware in routing) built into the app routing. For instance

app.all('/photo/*', requireAuthentication, loadUser);

app.get('/photo/view', function(req, res) {
   res.render('photo_view');
});

app.get('/photo/list', function(req, res) {
   res.render('photo_list');
});

其中 requireAuthentication loadUser 是函数。

查看app.VERB和app.all的文档在 http://expressjs.com/api.html#app.all

Take a look the documentation for app.VERB and app.all at http://expressjs.com/api.html#app.all

这篇关于filter.js上的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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