17

I have the following NodeJS code:

  let sql = `SELECT box_id, cubby_id, occupied, comport
           FROM box
           WHERE longestDimension = ?
           AND LOWER(box_id) = LOWER(?)`;

    connection.query(sql, [boxSelectedDimension, boxSelectedValue] , function(err, rows, fields) {
        if (!err) {
            for(var i=0; i< rows.length; i++) {
                // Make the comparaison case insensitive
                if (rows[i].occupied == `unoccupied`) {
                    console.log("free");

                    var comport = rows[i].comport;
                    var command = "open" + rows[i].cubby_id;

Essentially i would like to store the value of the comport and command variable in a session variable so that the value of these variable could be used in another router page in nodejs.

I am not sure how to store and retrieve the session variable.

2
  • are you using express? Commented Nov 23, 2016 at 3:46
  • yes i am using express Commented Nov 23, 2016 at 3:48

1 Answer 1

32

Install express-session and use as follows:

var express = require('express');
var session = require('express-session');
var app = express();
app.use(session({secret:'XASDASDA'}));
var ssn ;
app.get('/',function(req,res){
    ssn=req.session;
   /*
   * Here we have assign the 'session' to 'ssn'.
   * Now we can create any number of session variable we want.    
   * Here we do like this.
   */
   // YOUR CODE HERE TO GET COMPORT AND COMMAND
   ssn.comport; 
   ssn.command; 
});

Following code explain simple login and logout using session. The session we initialize uses secret to store cookies. Hope this helps.

var ssn;
app.get('/',function(req,res) { 
  ssn = req.session; 
  if(ssn.email) {
    res.redirect('/admin');
  } else {
    res.render('index.html');
  }
});
app.post('/login',function(req,res){
  ssn = req.session;
  ssn.email=req.body.email;
  res.end('done');
});
app.get('/admin',function(req,res){
  ssn = req.session;
  if(ssn.email) {
    res.write('<h1>Hello '+ssn.email+'</h1>');
    res.end('<a href="+">Logout</a>');
  } else {
    res.write('<h1>login first.</h1>');
    res.end('<a href="+">Login</a>');
  }
});
app.get('/logout',function(req,res){
  req.session.destroy(function(err) {
    if(err) {
      console.log(err);
    } else {
      res.redirect('/');
    }
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. What would be the purpose of app.use(session({secret:'XASDASDA'})); and how do i set the variables values?
secret is used for cookie handling etc and we put some secret to use express session and normally for different views we authenticate the session before routing to he views based on the condition and requirement.
okay perfect thanks for that, but how i would i define the session value of comport and command to be use in that page?

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.