node.js - Express dynamic include route based on user type -




need or clue including/requiring dynamic routes @ runtime in express, confusing try best.

this example of app routes configuration right now

    app.use('/',    require('./routes/public'));     app.use('/u',   require('./routes/user'));     app.use('/a',   require('./routes/admin')); 

for example require('./routes/public') include

router.get('/',                     home.index);                              router.get('/faq',                  faq.index)   

also require('./routes/user') include this

router.get('/dashboard',            user_home.index);                              router.get('/accounts',             user_acc.index) 

also require('./routes/admin')

router.get('/dashboard',            adm_home.index);                              router.get('/accounts',             adm_acc.index) 

i have installed passportjs easy check if user authenticated, if is, user contains 1 property type, eg: 1 = user, 2 = admin.

req.user.type = 1 or 2, req.isauthenticated()...

what need inject depending user type 1 or 2, require('./routes/user') or require('./routes/admin') @ runetime, cos dont want declare invalid routes user type user example including admin, or backwards.

right now, routes visible or valid need check every controller user type, dont want '/a' or '/u' routes prefix. routes must under '/'.

request runs first matched path. can render page, throw error or call next next middleware. below router examples.

'use strict' var express = require ('express'); var app = express();  var publicrouter = express.router(); publicrouter.get('/', (req, res) => res.send('root')); publicrouter.get('/account', function (req, res, next) {     if (!is-user)         res.send('hello guest')     else         next(); });  var userrouter = express.router(); function isuser (req, res, next) {     // return next() on user logon, , error otherwise     return next();   } userrouter.use(isuser); userrouter.get('/dashboard', (req, res) => res.send('/dashboard')); userrouter.get('/account', (req, res) => res.send('hello user'));  var adminrouter = express.router(); function isadmin (req, res, next) {     // return next() on admin logon, , error otherwise     return next(new error('access denied')); } adminrouter.get('/manage', (req, res) => res.send('/manage'));  app.use(publicrouter); app.use(userrouter); app.use('/admin', isadmin, adminrouter); // "/admin/manage", not "/manage"  // error handler app.use(function (err, req, res, next) {         res.send(err.message); });  app.listen(2000, () => console.log('listening on port 2000')); 

another way is

app.get('/', do-smth); app.get('/dashboard', isuser, do-smth); app.get('/manage', isadmin, do-smth); 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -