javascript - Express Router not picking pages from app.js -


i started basic sample of express , node. able set mongodb well.now problem comes in when try refactor code different modules i.e index,login , register.i have put different mechanisms different files , added routing same.but not pick files ( apart index page).

i attaching source code app.js .

var config = require('./config'); var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var mongoose = require('mongoose'); var passport = require('passport'); var localstrategy = require('passport-local').strategy; var routes = require('./routes/index'); var session=require('express-session');  var login = require('./routes/login'); var register = require('./routes/register');   var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade');    // uncomment after placing favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); app.use(cookieparser());  //session , passport initialization app.use(session({ secret: 'quifers', resave: false, saveuninitialized: false })); app.use(passport.initialize()); app.use(passport.session()); app.use(express.static(path.join(__dirname, 'public')));  // define routes here app.use('/', routes);  app.use('/login',login); app.use('/register',register);  var account = require('./models/account'); passport.use(new localstrategy(account.authenticate())); passport.serializeuser(account.serializeuser()); passport.deserializeuser(account.deserializeuser());  // mongoose //mongoose.createconnection(config.db.url); mongoose.connect(config.db.url); var db = mongoose.connection; db.on('error', function () { console.error('database connection error:'); process.exit(1); }); db.once('open', function (callback) { debugger;   // yay!  });  // catch 404 , forward error handler app.use(function(req, res, next) {  var err = new error('not found');  err.status = 404;   next(err); });   // error handlers  // development / test error handler // print stacktrace if (config.env.development || config.env.test) {   app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', {   message: err.message,   error: (config.env.development || config.env.test) ? err : {} }); }); }     module.exports = app; 

my login.js inside views folder looks :

var passport = require('passport'); var account = require('../models/account'); var express = require('express'); var router = express.router();  router.get('/login', function(req, res) {   res.render('login', { user : req.user }); });  router.post('/login', passport.authenticate('local'), function(req, res) {   res.redirect('/');  });  module.exports=router; 

but when go login, reloads page , redirects same /login route , says not found.

i newbie express, appreciated .thanks

the reason why doesn't work because route you're trying access doesn't exist.

login.js mounted under '/login', in login.js file redeclare /login 1 more time, end having route : /login/login

to make work, replace "/login" "/".

your code should :

var passport = require('passport'); var account = require('../models/account'); var express = require('express'); var router = express.router();  router.get('/', function(req, res) {   res.render('login', { user : req.user }); });  router.post('/', passport.authenticate('local'), function(req, res) {   res.redirect('/');  });  module.exports=router; 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -