node.js - Using Express 4 how to redirect to my own route without losing req and response data? -


i have application structured 3 routes (api, admin, default). each lives in there own file , has it's own middleware , exports route. problem facing when want forward route lives on different router. want call same function not serving same view multiple locations.

i don't want user res.redirect('/someplace') because want able pass req , res objects on method.

|-app.js |-routes   |---admin.js   |---api.js   |---default.js 

the routes required , used in app.js follows

app.use('/api', require('./routes/api')(passport); app.use('/admin', require('./routes/admin')(passport); app.use('/', require('./routes/default')(passport); 

inside of admin if have situation need redirect login , pass data

// authenticates routes admin router router.use(function(req, res, next){     if(req.isauthenticated()){         return next();     }     res.flashmessage.push('session expired'); //is lost after redirect     res.redirect('/login');     //do need restructure whole app don't     //have call res.redirect('login') }); 

any ideas on how structure this? need export every method , keep of routes in 1 router file? doesn't clean, if functions somewhere else may messy.

you can forward calling next callback ,but if not use paths.

app.use(function(req, res, next) {     // ... api     next(); });  app.use(function(req, res, next) {     // ... admin     next(); }); 

another option use * match paths:

app.use("*", function(req, res, next) {     var path = req.path; // example how can done      if (path === "/api") {        // ...        path = "/admin";     }      if (path === "/admin") {        // ...     } }); 

edit:

i don't think express has next('/login'); ,so function can forward request path , don't think right have this. if client ask /admin should send particular page , not page under /login. if want send client login page redirect did in question. understand want keep req, res ,but problem in proposal/structure of webapp.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -