Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Young
Courses Plus Student 639 PointsHow to serve different route for different user role
I'm building a community with express and I want to have different route with different role
I have four roles Admin, Teacher, Student, Visitor
How do I serve different route with those different roles\
I came up with two ideas first
- Use different app
var express = require('express');
var adminapp = express();
//Admin route here
adminapp.get('/', function(req, res) {
res.send("Admin route");
});
var trapp = express()
//Teacher app
trapp.get('/', function(req, res) {
res.send("Teacher route");
});
var http = require('http');
if (role == 'teacher'){
var httpServer = http.createServer(trapp);
} else {
var httpServer = http.createServer(adminapp);
}
httpServer.listen(8080);
But with this sample I don't know how to server route after recognizing their role
- Use logic
var express = require('express');
var app = express();
app.get('/', function(req, res) {
if (role == 'teacher') {
res.send("Teacher");
} else {
res.send("Other"):
}
});
var http = require('http');
var httpServer = http.createServer(app);
httpServer.listen(8080);
This is the last solution for my situation because it'll make code more complex to read and fix
So how do I server different route to different role with SIMPLE code
3 Answers
Alexander La Bianca
15,960 PointsHi,
Just a very basic way you could think of doing it is by redirecting the user based on their role. Let's say they access your home route '/'. You will have a some logic in place to make sure the user is an admin or a student. If an admin, redirect to /admin for example. If student redirect to /student.
app.get('/',function(req,res){
//do stuff here to determine if admin or student
if(role === 'admin') {
return res.redirect('/admin');
}
else {
return res.redirect('/student');
}
});
app.get('/admin',function(req,res) {
//do all admin response here
});
app.get('/student',function(req,res){
//do all student response here
});
Keep in mind that is is just very basic to give you an idea on how to think about it. You don't need to create 2 different servers to serve different routes. You don't need to create 2 different express instances either.
Alexander La Bianca
15,960 PointsEven with that approach you would not need separate express instances. I'd suggest splitting up your 3 groups into separate route modules with express.Router(). You can then treat each group as a 'mini app'. It will also be much easier to test and expand upon.
Andrew Young
Courses Plus Student 639 PointsDo you meant create another .js file write route in it export with something like module.export? and then use it with app.use?
That would be familiar with my first solution but there's question again how do I serve different route after I recognize user's role??
Can this work?
var express = require('express');
var app = express();
app.get('/login', function(req, res) {
res.send(loginform);
});
app.post('/login/request', function(req, res) {
//handle login request
//set session with username and role
});
app.get('/', function(req, res) {
//setup session settings
if (sess.role == 'admin') {
require('./routes/admin')(app);
} else {
require('./routes/tr')(app);
}
});
But can this work? Can you serve route setting after recognizing user's role after they login
Alexander La Bianca
15,960 PointsUse app.use() to mount your routes.
var express = require('express');
var app = express();
//require your routes
var admin = require('./routes/admin');
var teacher = require('./routes/teacher');
//mount them
app.use('/admin',admin);
app.use('/teacher',teacher);
app.get('/login', function(req, res) {
res.send(loginform);
});
app.post('/login/request', function(req, res) {
//handle login request
//set session with username and role
});
app.get('/', function(req, res) {
//setup session settings
if (sess.role == 'admin') {
//redirect them to /admin which will execute code in your admin.js
res.redirect('/admin');
} else {
res.redirect('/teacher');
}
});
Andrew Young
Courses Plus Student 639 PointsYeah, I know this way work and it's kind of the emergency method,
I'll use it when it's really no solution,
Now I want to know can I serve different route without using redirect so the URL bar will remain with the routing rule I provide above
Andrew Young
Courses Plus Student 639 PointsAndrew Young
Courses Plus Student 639 PointsOh sorry that I don't explain app's full process and design
the app is designed for multi-group(class)
the
teacherrole will have the following path designed (notice teacher role only have privilege to view path for it's own created class)For student I want to minify the path design (since they can only access their class
For admin the most powerful and complex path (also can access all classes)