クライアント側から index.jade にリダイレクトすることはできません。サーバーによって処理される場所にリダイレクトし、それに応じて index.jade ファイルをレンダリングしてクライアントに送信する必要があります。
// First, create an express server..
var express = require('express');
var app = express();
// Configure your server, make sure to put the correct paths
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
// You'll need express.session to manage logged in users.
app.use(express.session({
secret: "TOP SecerT",
store: new require('connect')()
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Now manage your routes..
// Instead of opening login.html using fs, just put your main content to the index.jade
// and render it as follows...
// For all the requests, if the user is not logged in, redirect to '/login'
app.use(function(req, res, next) {
if (! req.session.loggedIn)
return res.redirect('/login');
next();
});
app.use('/', function(req, res, next){
res.render('index.jade');
});
// Now, handle login requests here..
app.get('/login', function(req, res){
// Assuming that you put your login code into a template called login.jade
res.render('login.jade');
// OR if you don't have a login template just use res.sendfile('login.html');
});
// In your client side, you need to post the login credentials to the server,
// and here's the handler for that.
app.post('/login', function(req, res){
// Check the user credentials. This is normally getting done by querying a db, but
// let's make it simple here.
if (req.param('username') == 'foo' && req.param('password') == 'bar')
req.session.loggedIn = true;
});
// A simple handler for logout requests..
app.get('/logout', function(req, res){
// This will delete all your session data...
req.session = null;
});