javascript - Node JS Route Behavior makes no sense -
javascript - Node JS Route Behavior makes no sense -
i'm trying create simple authentication page using node express framework.
at moment, i'm not particularly worried weak protection.
however, route file displaying weird behavior.
here code:
var express = require('express'); var router = express.router(); var admin = require("../controllers/admin"); router.get('/', function(req, res) { console.log('entered top route.'); if(req.body.isauthenticated) { res.redirect('admin/index'); } else { res.redirect('admin/login'); } }); router.post('/authenticate', function(req,res) { if(req.body.pword == "key") { console.log("resetting body.isauth'ed"); req.body.isauthenticated = true; console.log("new val: " + req.body.isauthenticated); res.redirect('admin/index'); } else { console.log("failed login attempt: " + req.body.pword); res.redirect('admin/login'); } }); router.get('/login', admin.login); router.get('/index', admin.index); module.exports = router;
the thought route entered, upon nail top route (properly logged console). redirects based on whether req variable set. works properly, , login (i'm using jade templating) rendered. login file has form calls routing file 'admin/authenticate'. works properly; i'm able retrieve input (confirmed via console logs). however, actions take in /authenticate/ function, though exact same thing in '/' route right above, fail. 404's , express not found printouts exclusively useless.
my console logs 'failed login attempt' or 'resetting body...new val: true", doing exact same res.redirect done in router.get('/') method above fails, whereas res.redirects in function redirect , render right values.
any ideas wrong? post admin.login/admin.index methods in controllers/admin file. however, they're simple , think error stems basic misunderstanding of difference between , post methods or (this first time using node).
thanks!
i think issue because of relative path. doing res.redirect('admin/index')
path /
, path /authenticate
not same.
res.redirect('admin/index')
/authenticate
redirect user /authenticate/admin/index
, may thats why giving 404.
try alter res.rediret('admin/index')
res.redirect('/admin/index')
in authenticate callback.
javascript node.js express
Comments
Post a Comment