I currently am doing up a very simple site. It has the following structure:
/
-routes
--index.js
--login.js
--logout.js
-views
--index.jade
--login.jade
This site utilizes the latest version node.js, with the jade templating engine and express if that helps as well.
Basically I have it where when you click on index, your'e presented with the option to login, where it will process the login template, and check your credentials. With logout, I want it to just go to the .js file, nuke the session that I have, and reroute to the index view. However I can't seem to get the routing done correctly, it acts like it's expecting a view, but as there is none, I think that's why it's failing. Is there any way to say "no there's no view, jsut process the js file?"
EDIT: app.js file contents
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var indexRoute = require('./routes/index');
var loginRoute = require('./routes/login');
var logoutRoute = require('./routes/logout');
var app = express();
var db = require("mongojs").connect("localhost:27017/acl", ["comments"]);
app.use(function(req,res,next){
req.db = db;
next();
});
// view engine setup
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.get('*', function(req, res, next) {
res.locals.user = req.user || null;
next();
});
app.use('/', indexRoute);
app.post('/loginForm', loginRoute);
app.use('/login', loginRoute);
app.use('/logout', logoutRoute);
logout.js
var express = require('express);
var router = express.Router();
router.get('/logout', function (req, res) {
if (req.session === undefined) {
res.location("index");
res.redirect("/");
} else {
if (res.session.role == "guest") {
res.location("index");
res.redirect("/");
} else {
req.session = null;
res.render('home', {title: "express" });
}
}
}
module.exports = router;
It seems that your error is here:
...
} else {
req.session = null;
res.render('home', {title: "express" });
}
...
I believe you just want to do exactly the same thing as in the other parts of the if statement - redirect to the index.
On another note,
res.location("index");
res.redirect("/");
can and most likely should be changed to just
res.redirect("/")
Also there seems to be a typo on your first line in logout.js
- you are missing an end quote on your require('express')