node.js - Unable to perform an action if a view isn't present when clicking a link -
i doing simple site. has following structure:
/
-routes
--index.js
--login.js
--logout.js
-views
--index.jade
--login.jade
this site utilizes latest version node.js, jade templating engine , express if helps well.
basically have when click on index, your'e presented option login, process login template, , check credentials. logout, want go .js file, nuke session have, , reroute index view. can't seem routing done correctly, acts it's expecting view, there none, think that's why it's failing. there way "no there's no view, jsut process 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 error here:
... } else { req.session = null; res.render('home', {title: "express" }); } ...
i believe want same thing in other parts of if statement - redirect index.
on note,
res.location("index"); res.redirect("/");
can , should changed
res.redirect("/")
also there seems typo on first line in logout.js
- missing end quote on require('express')
Comments
Post a Comment