0

I am playing with node and was trying to set a cookie on a request but am getting an undefined exception. Here is my 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 that I am getting and returning the cookies:

enter image description here

But the result whenever I go to /users:id (where :id is obviously some number) I get a message saying the cookies object is undefined.

TypeError: Cannot read property 'cart' of undefined
    at c:\Projects\app\app.js:29:42
    at callbacks (c:\Projects\app\node_modules\express\lib\router\index.js:161:37)
    at param (c:\Projects\app\node_modules\express\lib\router\index.js:135:11)
    at param (c:\Projects\app\node_modules\express\lib\router\index.js:132:11)
    at pass (c:\Projects\app\node_modules\express\lib\router\index.js:142:5)
    at Router._dispatch (c:\Projects\app\node_modules\express\lib\router\index.js:170:5)
    at Object.router (c:\Projects\app\node_modules\express\lib\router\index.js:33:10)
    at next (c:\Projects\app\node_modules\express\node_modules\connect\lib\proto.js:199:15)
    at Object.expressInit [as handle] (c:\Projects\app\node_modules\express\lib\middleware.js:31:5)
    at next (c:\Projects\app\node_modules\express\node_modules\connect\lib\proto.js:199:15)

I've read all the other SO questions about putting the cookieParser above the other middleware, and from all accounts this example SHOULD work, but I'm at a loss as to what is missing.

1 Answer 1

1

Ok, turns out that it has to do with how I set up app.configure. The configure callback function wasn't getting called because the internals of app configure weren't calling the initialization function for the production ENV even though I thought it was explicitly set above.

To fix that, I changed the process.env.NODE_ENV to app.settings.env and everything started to work.

Found that info here: how to find out the current NODE_ENV the express app is running under?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.