Node JS – Session stored in MySQL (Sails JS)

In order to have the session stored in MySQL we can use the npm module “express-mysql-session”, that works directly with the Express JS framework that lies under Sails JS.

To configure our session we have to edit file “config/session.js” file in this way:

var MySQLSessionStore = require('express-mysql-session');

module.exports.session = {
     ...
     
     store: new MySQLSessionStore({
          host: 'localhost',
          port: 3306,
          user: 'root',
          password: 'root',
          database: 'test-node-sails'
     })
}

We can save data into the session from any controller through req.session:

req.session.user = { … };
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s