Express JS Routing

Asked

Viewed 65 times

1

I am new to using Express JS and would like to make a direction according to the session created, IE, according to the user profile, the application has an initial route "audit" and will have 2 user profiles and I want the express to call the route file corresponding to the profile, basically would be more or less like this:

var routerAdmin_1 = require('./routes/admin_1');
var routerAdmin_2 = require('./routes/admin_2');

app.use('/auditoria', (req, res, next) => {
  if(req.session.cargo == 'ADMIN_1'){
    chamar routerAdmin_1 
  }else if(req.session.cargo == 'ADMIN_2'){
    chamar routerAdmin_2 
  }else{
    console.log('Acesso negado')
  }

});

2 answers

1

0


I modified your code, as follows:

var routerAdmin_1 = require('./routes/admin_1');
var routerAdmin_2 = require('./routes/admin_2');

app.use('/auditoria', (req, res, next) => {
  if(req.session.cargo == 'ADMIN_1'){
    res.redirect(routerAdmin_1caminho) 
  }else if(req.session.cargo == 'ADMIN_2'){
    res.redirect(routerAdmin_2caminho)
  }else{
    console.log('Acesso negado')
  }

});

Now, just replace it routerAdmin_1caminho and routerAdmin_2caminho at their respective addresses.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.