J
J
John2014-08-24 22:45:38
Node.js
John, 2014-08-24 22:45:38

Session persistence in NodeJs?

Tell me how to save the session in NodeJs correctly, I read that express has work with sessions by default, but how to work with it is not described anywhere. Or maybe you can advise what is better, maybe some additional package for working with sessions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
John Doe, 2014-08-26
@Homchenkokostya

Here is my code (not working) throws an error
app.js file

var express = require('express');
var session = require('express-session');
var SessionStore = require('express-mysql-session');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'theme'));
app.set('view engine', 'ejs');

app.use(express.bodyParser());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser());

var options = {
    host: '127.0.0.1',
    port: 3000,
    user: 'nodejs',
    password: 'c5c58f',
    database: 'nodejs'
}

app.use(session({
    key: 'session_cookie_name',
    secret: 'session_cookie_secret',
    store: new SessionStore(options)
}))

app.get('/', function(req, res, next) {
    req.session.number = req.session.number + 1 || 1;
    console.log(req.session.number)
})

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

What could be the error, I have not been able to solve this problem for the second week. Is it really impossible in NodeJs to implement saving the session in the MySql database. Everywhere they give an example with saving to MongoDB, maybe NodeJs is really so limited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question