All Projects → chill117 → Express Mysql Session

chill117 / Express Mysql Session

Licence: mit
A MySQL session store for the express framework in node

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Express Mysql Session

Jebena
Lightweight JSON validation library
Stars: ✭ 56 (-79.1%)
Mutual labels:  express, express-middleware
Express Joi Validation
validate express application inputs and parameters using joi
Stars: ✭ 70 (-73.88%)
Mutual labels:  express, express-middleware
Celebrate
A joi validation middleware for Express.
Stars: ✭ 1,041 (+288.43%)
Mutual labels:  express, express-middleware
Express Openapi Validator
🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
Stars: ✭ 436 (+62.69%)
Mutual labels:  express, express-middleware
Vue Shoppingcart
ShoppingCart (Ecommerce) 🛒 Application using Vuejs, + Node.js + Express + MongoDB 🚀🤘
Stars: ✭ 141 (-47.39%)
Mutual labels:  express, express-middleware
Mern Login Signup Component
Minimalistic Sessions based Authentication app 🔒 using Reactjs, Nodejs, Express, MongoDB and Bootstrap. Uses Cookies 🍪
Stars: ✭ 74 (-72.39%)
Mutual labels:  express, sessions
Webauthn
W3C Web Authentication API Relying Party for Node.js and Express
Stars: ✭ 61 (-77.24%)
Mutual labels:  express, express-middleware
Host Validation
Express.js middleware for "Host" and "Referer" header validation to protect against DNS rebinding attacks.
Stars: ✭ 183 (-31.72%)
Mutual labels:  express, express-middleware
Join Io
join files on a fly to reduce requests count
Stars: ✭ 80 (-70.15%)
Mutual labels:  express, express-middleware
Memorystore
express-session full featured MemoryStore layer without leaks!
Stars: ✭ 79 (-70.52%)
Mutual labels:  express, sessions
Connect Session Sequelize
Sequelize SessionStore for Express/Connect
Stars: ✭ 179 (-33.21%)
Mutual labels:  express, sessions
Express Basic Auth
Plug & play basic auth middleware for express
Stars: ✭ 241 (-10.07%)
Mutual labels:  express, express-middleware
npm-sharper
📷 Automatic image processor middleware built on top of sharp and multer for express.
Stars: ✭ 17 (-93.66%)
Mutual labels:  express-middleware
Nextjs Redux Starter
Next.js + Redux + styled-components + Express = 😇
Stars: ✭ 257 (-4.1%)
Mutual labels:  express
neovim-session-manager
A simple wrapper around :mksession
Stars: ✭ 148 (-44.78%)
Mutual labels:  sessions
moesif-nodejs
Moesif Nodejs Middleware Library (formerly Moesif-Express)
Stars: ✭ 36 (-86.57%)
Mutual labels:  express-middleware
Woodlot
An all-in-one JSON logging utility that supports ExpressJS HTTP logging, custom logging, provides multi-format output and an easy to use events API.
Stars: ✭ 263 (-1.87%)
Mutual labels:  express-middleware
stats
📊 Request statistics middleware that stores response times, status code counts, etc
Stars: ✭ 15 (-94.4%)
Mutual labels:  express-middleware
express-graphql
Create a GraphQL HTTP server with Express.
Stars: ✭ 6,301 (+2251.12%)
Mutual labels:  express-middleware
BEW-1.3-Server-Side-Architectures-and-Frameworks
🔐 Build on knowledge of Resourceful and RESTful patterns and dive deep into the Node and Express ecosystem.
Stars: ✭ 19 (-92.91%)
Mutual labels:  sessions

express-mysql-session

A MySQL session store for express.js.

Build Status

Installation

Add to your application via npm:

npm install express-mysql-session --save

This will install express-mysql-session and add it to your application's package.json file.

Important Notes

Potential gotchas and other important information goes here.

Older Versions

For users who are still using express-mysql-session 0.x. Changes have been made to the constructor, which are backwards compatible, but you could run into troubles if using an older version of this module with the latest documentation. You can find the documentation for the older version here.

Session Table Collation

This module creates a database table to save session data. This data is stored in a MySQL text field with the utf8mb4 collation - added in MySQL 5.5.3. The reason for this is to fully support the utf8 character set. If you absolutely must use an older version of MySQL, create your sessions table before initializing the MySQLStore.

Usage

Use with your express session middleware, like this:

var express = require('express');
var app = module.exports = express();
var session = require('express-session');
var MySQLStore = require('express-mysql-session')(session);

var options = {
	host: 'localhost',
	port: 3306,
	user: 'session_test',
	password: 'password',
	database: 'session_test'
};

var sessionStore = new MySQLStore(options);

app.use(session({
	key: 'session_cookie_name',
	secret: 'session_cookie_secret',
	store: sessionStore,
	resave: false,
	saveUninitialized: false
}));

The session store will internally create a mysql connection pool which handles the (re)connection to the database. By default, the pool consists of 1 connection, but you can override this using the connectionLimit option. There are additional pool options you can provide, which will be passed to the constructor of the mysql connection pool.

The sessions database table should be automatically created, when using default options. If for whatever reason the table is not created, you can find the schema here.

With an existing MySQL connection or pool

To pass in an existing MySQL database connection or pool, you would do something like this:

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

var options = {
    host: 'localhost',
    port: 3306,
    user: 'db_user',
    password: 'password',
    database: 'db_name'
};

var connection = mysql.createConnection(options); // or mysql.createPool(options);
var sessionStore = new MySQLStore({}/* session store options */, connection);

Closing the session store

To cleanly close the session store:

sessionStore.close();

Options

Here is a list of all available options:

var options = {
	// Host name for database connection:
	host: 'localhost',
	// Port number for database connection:
	port: 3306,
	// Database user:
	user: 'session_test',
	// Password for the above database user:
	password: 'password',
	// Database name:
	database: 'session_test',
	// Whether or not to automatically check for and clear expired sessions:
	clearExpired: true,
	// How frequently expired sessions will be cleared; milliseconds:
	checkExpirationInterval: 900000,
	// The maximum age of a valid session; milliseconds:
	expiration: 86400000,
	// Whether or not to create the sessions database table, if one does not already exist:
	createDatabaseTable: true,
	// Number of connections when creating a connection pool:
	connectionLimit: 1,
	// Whether or not to end the database connection when the store is closed.
	// The default value of this option depends on whether or not a connection was passed to the constructor.
	// If a connection object is passed to the constructor, the default value for this option is false.
	endConnectionOnClose: true,
	charset: 'utf8mb4_bin',
	schema: {
		tableName: 'sessions',
		columnNames: {
			session_id: 'session_id',
			expires: 'expires',
			data: 'data'
		}
	}
};

Configurable sessions table and column names

You can override the default sessions database table name and column names via the schema option:

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

var options = {
	host: 'localhost',
	port: 3306,
	user: 'session_test',
	password: 'password',
	database: 'session_test',
	schema: {
		tableName: 'custom_sessions_table_name',
		columnNames: {
			session_id: 'custom_session_id',
			expires: 'custom_expires_column_name',
			data: 'custom_data_column_name'
		}
	}
};

var sessionStore = new MySQLStore(options);

Debugging

express-mysql-session uses the debug module to output debug messages to the console. To output all debug messages, run your node app with the DEBUG environment variable:

DEBUG=express-mysql-session* node your-app.js

This will output log messages as well as error messages from express-mysql-session.

If you also might need MySQL-related debug and error messages, see debugging node-mysql.

Contributing

There are a number of ways you can contribute:

  • Improve or correct the documentation - All the documentation is in this readme file. If you see a mistake, or think something should be clarified or expanded upon, please submit a pull request
  • Report a bug - Please review existing issues before submitting a new one; to avoid duplicates. If you can't find an issue that relates to the bug you've found, please create a new one.
  • Request a feature - Again, please review the existing issues before posting a feature request. If you can't find an existing one that covers your feature idea, please create a new one.
  • Fix a bug - Have a look at the existing issues for the project. If there's a bug in there that you'd like to tackle, please feel free to do so. I would ask that when fixing a bug, that you first create a failing test that proves the bug. Then to fix the bug, make the test pass. This should hopefully ensure that the bug never creeps into the project again. After you've done all that, you can submit a pull request with your changes.

Before you contribute code, please read through at least some of the source code for the project. I would appreciate it if any pull requests for source code changes follow the coding style of the rest of the project.

Now if you're still interested, you'll need to get your local environment configured.

Configure Local Environment

Step 1: Get the Code

First, you'll need to pull down the code from GitHub:

git clone https://github.com/chill117/express-mysql-session.git

Step 2: Install Dependencies

Second, you'll need to install the project dependencies as well as the dev dependencies. To do this, simply run the following from the directory you created in step 1:

npm install

Step 3: Set Up the Test Database

Now, you'll need to set up a local test database:

{
	host: 'localhost',
	port: 3306,
	user: 'session_test',
	password: 'password',
	database: 'session_test'
};

The test database settings are located in test/config.js

Alternatively, you can provide custom database configurations via environment variables:

DB_HOST="localhost"
DB_PORT="3306"
DB_USER="session_test"
DB_PASS="password"
DB_NAME="session_test"

Tests

This project includes an automated regression test suite. To run the tests:

npm test

Changelog

See changelog.md

License

This software is MIT licensed:

A short, permissive software license. Basically, you can do whatever you want as long as you include the original copyright and license notice in any copy of the software/source. There are many variations of this license in use.

Funding

This project is free and open-source. If you would like to show your appreciation by helping to fund the project's continued development and maintenance, you can find available options here.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].