Skip to content

Instantly share code, notes, and snippets.

@milani
Created June 8, 2012 14:46
Show Gist options
  • Save milani/2895990 to your computer and use it in GitHub Desktop.
Save milani/2895990 to your computer and use it in GitHub Desktop.
var app = express.createServer();
var publicPath = './public';
var adminPath = './admin';
app.use(express.static( path.resolve( publicPath ) ) );
app.use(function(req,res,next){
// if req.path starts with /admin/ and user is logged in call next
// else redirect to login page
var userIsLoggedIn = true; // check session etc.
if( req.path.indexOf('/admin/') == 0 && userIsLoggedIn ) {
next();
} else {
// ask user for login
}
});
app.use('/admin',express.static( path.resolve( adminPath ) ) );
app.listen(3000);
@cosminnicula
Copy link

...
app.use(express.static( path.resolve( publicPath ) ) );

app.use(function(req, res, next) {
if(req.path.indexOf('/admin') === 0) {
if(req.session && req.session.user) {
next();
} else {
console.log('login failed');
res.redirect('/#login');
}
} else {
//somehow it should skip here the app.use('/admin', express.static('./admin')); line.....
}
});
app.use('/admin',express.static( path.resolve( adminPath ) ) );
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment