node.js - How to serve a different home page with Express? -
is possible serve different home page user depending if they're logged in or not?
for example, app uses:
app.use(express.static(__dirname + '/public'));
which serves index.html
home page.
although serve login.html
home page if user not authenticated, , app.html
if user authenticated. using middleware.
can done expressjs?
you can try following approach :
app.get('/', function(req, res, next) { try { var contents, authenticated; // user logged-in or not. authenticated = true/false; if(!authenticated) { contents = require('fs').readfilesync(require('path').join(__dirname, './login.html')).tostring(); } else { contents = require('fs').readfilesync(require('path').join(__dirname, './index.html')).tostring(); } res.setheader('content-type', 'text/html'); res.send(new buffer(contents)); } catch(ex) { res.setheader('content-type', 'application/json'); res.send(json.stringify(ex)); } });