Express.js and Node.js : Unable to set value in the request object -


i following practical node.js book. based on older version on express.js. book trying build blog. has several different routes files. ex- index.js, admin.js, article.js etc. route classes called app.js. ex:

app.use('/', routes.index);//// issue here ///// app.get('/login', routes.user.login); app.post('/login', routes.user.authenticate); app.get('/logout', routes.user.logout); app.get('/admin',  routes.article.admin); app.get('/post',  routes.article.post); app.post('/post', routes.article.postarticle); 

whenever tries access '/', setting collections object of artices , users in request object.

var dbarticles = require('./db/articles.json'); var dbusers = require('./db/users.json'); app.use(function(req, res,next) {   if (!collections.articles || ! collections.users) return next(new error("no collections."))   req.collections = collections;   return next(); });   app.use('/', routes.index);//// issue here ///// 

the problem in index.js file, value of req.collections no available, 'undefined'. missing. have checked in console.log, value present in req.collections before '/', route.index hit.

here's app.js

    var express = require('express'); var router = express.router(); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var session = require('express-session'); var  methodoverride = require('method-override'); var routes = require('./routes'); var dbarticles = require('./db/articles.json'); var dbusers = require('./db/users.json');  var collections = {     articles: dbarticles,     users: dbusers   }; var logger = require('morgan');  var errorhandler = require('errorhandler');  var app = express();    app.locals.apptitle = 'blog-express';  //console.log(collections.articles || collections.users);    app.use(function(req, res,next) {   if (!collections.articles || ! collections.users) return next(new error("no collections."))   req.collections = collections;   return next(); });    // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade');  // uncomment after placing favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded()); app.use(methodoverride()); app.use(require('stylus').middleware(__dirname + '/public')); app.use(express.static(path.join(__dirname, 'public')));  // pages , routes app.use('/', routes.index);  app.get('/login', routes.user.login); app.post('/login', routes.user.authenticate); app.get('/logout', routes.user.logout); app.get('/admin',  routes.article.admin); app.get('/post',  routes.article.post); app.post('/post', routes.article.postarticle); app.get('/articles/:slug', routes.article.show);  // rest api routes app.get('/api/articles', routes.article.list); app.post('/api/articles', routes.article.add); app.put('/api/articles/:id', routes.article.edit); app.delete('/api/articles/:id', routes.article.del);    app.all('*', function(req, res) {   res.sendstatus(404); }) // catch 404 , forward error handler   // error handlers  // development error handler // development if ('development' == app.get('env')) {   app.use(errorhandler()); }  // production error handler // no stacktraces leaked user   //module.exports = router; module.exports = app; 

here index.js

exports.article = require('./article'); exports.user = require('./user');  /*  * home page.  */  exports.index = function(req, res, next){     console.log(".." + res.collections);   req.collections.articles.find({published: true}, {sort: {_id:-1}}).toarray(function(error, articles){     if (error) return next(error);     res.render('index', { articles: articles});   }) }; 

if 1 needs take @ code base of book, please check github url - https://github.com/azat-co/practicalnode/tree/master/ch5/blog-express

try changing app.use('/', routes.index) app.get('/',routes.index)

if doesn't work, try setting middleware inline...

var setcollections  = function (req, res,next) {   if (!collections.articles || ! collections.users) return next(new error("no collections."))   req.collections = collections;   return next(); }  app.get('/', setcollections, routes.index) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -