1

I am getting the following error while trying to run node server by using the command node server using node.js.

Error:

express-session deprecated undefined resave option; provide resave option server
.js:18:9
express-session deprecated undefined saveUninitialized option; provide saveUnini
tialized option server.js:18:9
C:\xampp\htdocs\cit_node\node_modules\express\lib\router\route.js:196
        throw new Error(msg);
        ^

Error: Route.get() requires callback functions but got a [object Undefined]
    at Route.(anonymous function) [as get] (C:\xampp\htdocs\cit_node\node_module
s\express\lib\router\route.js:196:15)
    at EventEmitter.app.(anonymous function) [as get] (C:\xampp\htdocs\cit_node\
node_modules\express\lib\application.js:481:19)
    at Object.<anonymous> (C:\xampp\htdocs\cit_node\server.js:20:5)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:134:18)
    at node.js:961:3

I am explaining my server side code below.

server.js:

var port=8888;
var express=require('express');
var http=require('http');
var morgan         = require('morgan');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var expressSession = require('express-session');
var cookieParser = require('cookie-parser'); // the session is stored in a cookie, so we use this to parse it
var app            = express();
var server=http.createServer(app);
var admin=require('./route/route.js');
app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users
app.use(morgan('dev'));                     // log every request to the console
app.use(bodyParser.urlencoded({ extended: false }))    // parse application/x-www-form-urlencoded
app.use(bodyParser.json())    // parse application/json
app.use(methodOverride());                  // simulate DELETE and PUT
app.use(cookieParser());
app.use(expressSession({secret:'abcx'}));
app.post('/login',admin.adminlogin);
app.get('/session',admin.session);
//app.post('/logout',admin.logout);
app.get('/',function(req,res){
    res.sendfile('view/index.html');
});
server.listen(port);   
console.log('Magic happens on port'+port);          // shoutout to the user

Before some time i was able to run this but dont know why it is throwing this type of error.My package.json file is given below.

{
  "name": "Gofasto",
  "main": "server.js",
  "dependencies": {
    "express": "4.13.3",
    "morgan": "1.6.1",
    "body-parser": "1.14.1",
    "method-override": "2.3.5",
    "socket.io":"1.3.7",
    "mongojs":"1.4.1",
    "cookie-parser": "1.4.0",
    "express-session": "1.11.3"
  }
}

route.js:

var mongo = require('mongojs');
var database='go_fasto';
var collections=['user'];
var db=mongo("127.0.0.1:27017/"+database, collections);
exports.adminlogin=function(req,res){
    var username=req.body.username;
    var password=req.body.password;
    db.user.findOne({
        user_name:username,
        raw_password:password
    },function(err,docs){
        if(!err){
            if(docs){
                console.log('docs',docs);
                ses=req.session;
                ses.username=docs.user_name;
                ses.user_type=docs.user_type;
                ses.email_id=docs.email;
                ses.dept_id=docs.dept_id;
                res.send(docs);
            }
        }
        if(err){
            res.send("could not login");
        }
    });
}
/*exports.logout=function(req,res){
    req.session.destroy(function(err){
if(err){
console.log(err);
}
else
{
res.redirect('/');
}
});
}
*/

I am using the node version 4.2.1.Please help me to resolve this error.

1

1 Answer 1

3

As per comment look at this: https://github.com/expressjs/session/issues/56

You need to specify the resave and the saveUninitialized options:

app.use(expressSession({
    secret: 'abcx',
    resave: true,
    saveUninitialized: true
}));

Then the error refers to this line of code:

app.get('/session',admin.session);

Because admin.session is undefined in ./route/route.js

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

1 Comment

@ Michelem: Ok i added and the warning are gone but still error is there which has explained in my post.

Your Answer

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