Answer the question
In order to leave comments, you need to log in
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
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'));
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question