node.js - Determining path that express router is bound to without checking request headers -
this question similar this one, can't inspect req.baseurl suggested in answers. reason is, of code in question isn't inside callback.
i want have admin panel '/admin/' page redirecting '/admin/login' when user isn't authenticated.
const cel = require('connect-ensure-login'); const admin = express.router(); admin.get('/login', (req, res) => { res.render('login', { path: req.baseurl.splice(1) }); // form action needs know /admin/login path }); admin.post('/login', passport.authenticate('local', { failureredirect: '/admin/login' }), // above 1 of lines in question - since it's not inside (req, res) callback, // i'm unable access req.baseurl here (req, res) => { res.redirect(req.baseurl.splice(1)+'/'); // here full path needed redirect }); admin.get('/', cel.ensureloggedin('/admin/login'), // problematic line, same above (req, res) => { res.render('adminhome'); }); const app = express(); app.use('/admin', admin); many requires , middleware calls skipped conciseness.
how solve? hardcoding paths parameter: '/admin/login' seems against idea of modular structure.
Comments
Post a Comment