1

I run localhost:3000/name/john, create a cookie then localhost:3000/name to display the cookie I have a console error "TypeError: Can not read property 'name' of undefined"

look my code

 var express = require('express')
     , http = require('http') 
     , path = require('path') ;

var app = express();        

        app.configure(function(){
            app.set('port', process.env.PORT || 3000);
            app.use(express.bodyParser()) ;
            app.use(express.methodOverride()) ;
            app.use(app.router);

            app.use(express.cookieParser());  
            app.use(express.session({ secret: 'secret'}));
        }) ;

      app.get('/name/:name', function(req, res){
            res.cookie('name' , req.params.name).send('<p>the cookie <a href="/name">Go here</a></p>') ;
       });

      app.get('/name', function(req, res){
          console.log(req.cookies.name);
          res.send("index");
      });  

    http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });

how to solve this problem ? thanks

2

1 Answer 1

1

Modify your app.configure code as below.

app.configure(function(){
      app.set('port', process.env.PORT || 3000);
      app.use(express.bodyParser()) ;
      app.use(express.methodOverride()) ;
      app.use(express.cookieParser());  
      app.use(express.session({ secret: 'secret'}));
      app.use(app.router);
}) ;

Have a look at http://expressjs.com/api.html#app.use. Here the problem is with the middleware precedence.

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.