Wednesday, 15 June 2011

node.js - Undefined exception when getting cookie -



node.js - Undefined exception when getting cookie -

i playing node , trying set cookie on request getting undefined exception. here sample application

var express = require('express'); var app = module.exports = express(); process.env.node_env = 'production'; app.configure('production', function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.cookieparser()); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); app.get("/", function(req, res){ res.cookie('cart', 'test', {maxage: 900000, httponly: true}); res.send("ok"); }); app.get('/users/:id', function(req, res) { var s = req.params.id; res.send('testcookie: ' + req.cookies.cart); }); app.listen(3000); console.log('listening on port 3000');

i can validate in charles getting , returning cookies:

but result whenever go /users:id (where :id number) message saying cookies object undefined.

typeerror: cannot read property 'cart' of undefined @ c:\projects\app\app.js:29:42 @ callbacks (c:\projects\app\node_modules\express\lib\router\index.js:161:37) @ param (c:\projects\app\node_modules\express\lib\router\index.js:135:11) @ param (c:\projects\app\node_modules\express\lib\router\index.js:132:11) @ pass (c:\projects\app\node_modules\express\lib\router\index.js:142:5) @ router._dispatch (c:\projects\app\node_modules\express\lib\router\index.js:170:5) @ object.router (c:\projects\app\node_modules\express\lib\router\index.js:33:10) @ next (c:\projects\app\node_modules\express\node_modules\connect\lib\proto.js:199:15) @ object.expressinit [as handle] (c:\projects\app\node_modules\express\lib\middleware.js:31:5) @ next (c:\projects\app\node_modules\express\node_modules\connect\lib\proto.js:199:15)

i've read other questions putting cookieparser above other middleware, , accounts illustration should work, i'm @ loss missing.

ok, turns out has how set app.configure. configure callback function wasn't getting called because internals of app configure weren't calling initialization function production env though thought explicitly set above.

to prepare that, changed process.env.node_env app.settings.env , started work.

found info here: how find out current node_env express app running under?

node.js cookies express

No comments:

Post a Comment