0
    var express = require('express');
    var app = express();
        app.use(express.cookieParser('keyboard 123'));

app.get('/', function(req, res){
  res.sendfile('./login.html');
});

        app.use(express.session());

            app.post('/login', function(req, res){

            console.log(req.body);
            if (req.session.user) {
                req.session.user=req.body.username;
              } else {
                req.session.user="test";

              }

        });




<form id="myform" action="/login" method="post" enctype="application/x-www-form-urlencoded">
  <input type="text" name="username" id="mytext" />
  <input type="submit" id="mysubmit" />
</form>

I am trying to understand and use express node js . Here I have a form which does post. My understanding is that : the first time there is no cookie(session) available. so, when we get a request : req.session.user will be empty. so when we send 200 OK. this req.session.user cookie wil be created and stored in the local browser.? and subsequently in all the req this req.session object will be send is that correct?

Issue now is : I always get req.body.username as undefined. I have the same user name defined in the login page. I have tried console.log(req.body) and it is always undefined. if I have no session, the body part works.

can you clarify me if I am understanding is correct and what is the issue in this code. I am having really issues understandging the concept of session and how to use it in express frame work

The Learner
  • 3,867
  • 14
  • 40
  • 50

1 Answers1

1

It seems like you are confusing the request and response. If you post a larger code excerpt, we can give more detailed help, but here's my take on your misunderstanding.

  • When POST /login arrives, you could check if req.session.user is set. If so, the user is already logged in but logging in again, which is not a usual case, but you could do something like res.send('You are already logged in as ' + req.session.user);
  • If req.session.user is not set, the user is logging in. Normally you would confirm the user knows the right password, but for your little test app you would just do as you have req.session.user = req.body.username and respond with `res.send('You have been logged in as ' + req.session.user); for example.

Also just make sure you have the cookieParser, session, and bodyParser middlewares configured properly in your application.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Thanks peter. I updated the complete code.I dont have any code all I am looking for is in console to print. I am not getting any data printed for console.log(req.body); 2) so when I have the cookie and all set, than when I respond back req.session.user, the browser will atomatically take the req.session as a cookie and update that locally? that is the part I am but confused – The Learner Dec 25 '12 at 02:12
  • That is incomplete code. Where are your `require` statements? Do you actually tell express to use the `bodyParser` middleware? I think you are probably making easy mistakes to correct but since you won't post a complete code sample, I can't help you. – Peter Lyons Dec 25 '12 at 05:32
  • I added the complete code. here the req.body is always null..that is the first issue – The Learner Dec 25 '12 at 05:41
  • 1
    Add this line: `app.use(express.bodyParser());` That is the middleware whose job is to populate `req.body` automatically for you. – Peter Lyons Dec 25 '12 at 05:52
  • Thank you this worked. I have written many examples on express I did not use this before app.use(express.bodyParser()); and they worked. the only differnce is I never used app.use in those instances. so does it mean if I use once app.use in the program I should also you this? – The Learner Dec 25 '12 at 05:55
  • and my other question as I asked initially "req.session.user". so if I set in res.session.user (or) res.session.password it will automatically set them in the cookie right? – The Learner Dec 25 '12 at 05:58
  • 2
    You need to actually understand what the code you are writing does. You are getting each middleware all confused with one another. The `bodyParser`'s job is to parse incoming requests for you and populate `req.body`. The `session` middleware will populate `req.session` for you, but it won't store data other than the session ID in the cookie. However, you can store data in the `req.session` object and it will be available for you, but only the session ID is actually sent to the browser. – Peter Lyons Dec 25 '12 at 06:01
  • I was executing the code I have in teh question. it prints first time undefined, than my program assigns "test". when I re-run the same html page with out closing the browser, I get req.session.user as test which is assigned. so it looks what ever I have done in req.session.user is also send to the browser? I am not getting that part how/what all we send to the cookie. you mentioned only sessionid is send to the browser...I am bit confused about this cookie part what all is send and how it is send,I serached in google could not get good document \ – The Learner Dec 25 '12 at 06:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21670/discussion-between-the-learner-and-peter-lyons) – The Learner Dec 25 '12 at 07:27