当前位置: 首页 > 软件库 > Web应用开发 > >

express-mysql-session

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发
软件类型 开源软件
地区 不详
投 递 者 柴博
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

express-mysql-session

A MySQL session store for express.js.

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'
		}
	}
};

Custom database table schema

It is possible to use a custom schema for your sessions database table. This can be useful if you want to have extra columns (e.g. "user_id"), indexes, foreign keys, etc. You could also change the type of the "data" column to a smaller or larger text type (e.g. "TINYTEXT", "LONGTEXT", "BLOB") or native "JSON" type.

Set the createDatabaseTable option to FALSE so that the session store does not automatically create a sessions table.

Use the schema option to provide the custom table and column names to the session store.

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',
	createDatabaseTable: false,
	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);

With mysql2

This module is compatible with the mysql2 module. You will need to create and pass an instance of the mysql2 connection object as follows:

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

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

var connection = mysql2.createPool(options);
var sessionStore = new MySQLStore({}, connection);

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.

  • 2 You can use session (express-session) to achieve that. I currently using this combination express, express-session, express-mysql-session (for the storage), passport with facebook and twitter strate

  • 实例化时配置useConnectionPooling: true var session = require('express-session'); var mysql = require('mysql'); var MySQLStore = require('express-mysql-session')(session); var mysqlOption = { host: 'localh

  • 前言:最近在使用 express-session 中间件,查看使用的时候有些参数不是很清楚就花了一点时间把文档翻译了一下。 其中常用设置中令人迷糊的是 resave 和 saveUnintialized 属性,关于这两个属性,引用来自 CNODE 社区更通熟易懂的解释: resave : 是指每次请求都重新设置 session cookie,假设你的 cookie 是 10 分钟过期,每次请求都会

  • koa+vue 前后端分离登录凭证 (koa-mysql-session) 想用 koa+vue 实现前后端分离,但在这个登录凭证这边卡住不知道怎么继续下去,网上有很多方法,但都是express或者并不是前后端分离的session持久化,并不能完美套用。以下是我现在找到的前后端分离的session持久化的方法,希望能帮到你 node端 app.js const Koa = require('koa

  • 我有一个使用与MySQL的快速会话的NodeJs,Express应用程序。在我的Mac上,我为每个请求设置了一个新的sessionId。结果是,当应用程序正在查找以前写入会话的数据时,它不在那里,因为它在不同的会话上。相同的代码在Centos测试框上正常工作。 会话设置代码看起来如下所示: appConfig.db: db: { "host" : "localhost", "database":

  • session的使用 与cookie不同,session是保存在服务器上的数据。 session的工作流程: 当浏览器访问服务器并发起第一个请求时,服务器会创建一个session对象,生产一个类似于 key-value的键值对,然后将key(实际上是cookie)返回给客户端浏览器,浏览器下次访问 时,将携带保存在浏览器上的key,一起发送请求服务器找到相应的session(value) 使用步

 相关资料
  • Node.js Rest APIs with Express & MySQL example For instruction, please visit: Build Node.js Rest APIs with Express & MySQL More Practice Build Node.js Rest APIs with Express, Sequelize & MySQL Server

  • Node.js Rest APIs with Express, Sequelize & MySQL example For more detail, please visit: Build Node.js Rest APIs with Express, Sequelize & MySQL Server side Pagination in Node.js with Sequelize and My

  • 问题内容: Sun最近购买的MySQL及其后续的错误版本是否杀死了MySQL品牌? 当我刚开始是MySQL的时候,我全心全意地拥抱MySQL,因为我以前是一个贫穷的开发人员,并且所有RDBM都太昂贵了。我对MySQL以及它们能够与Oracle和SQL Server竞争感到很满意。我将最初的MySQL团队归功于SQL Server Express的存在。现在,我几乎使用SQL Server Expr

  • 本文向大家介绍Node.js+Express+Mysql 实现增删改查,包括了Node.js+Express+Mysql 实现增删改查的使用技巧和注意事项,需要的朋友参考一下 这次选用nodejs+express+mysql 使用http作为客户端,express框架搭建服务端,从而实现数据的增删改查。这篇文章可以算作上篇文章的升级篇,加入了和数据库的交互。 安装 node 直接去官网下载选择下载

  • 本文向大家介绍浅析node连接数据库(express+mysql),包括了浅析node连接数据库(express+mysql)的使用技巧和注意事项,需要的朋友参考一下 操作是在ubuntu系统的下环境,简单记录一下过程。 首先用apt-get安装数据库,键入命令 sudo apt-get install mysql-server , 一路回车,然后在一个界面设置一下数据库root的密码就好了 在数

  • 快递概述 Express是一个最小且灵活的Node.js Web应用程序框架,它提供了一组强大的功能来开发Web和移动应用程序。 它有助于基于节点的Web应用程序的快速开发。 以下是Express框架的一些核心功能 - 允许设置中间件以响应HTTP请求。 定义路由表,该表用于基于HTTP方法和URL执行不同的操作。 允许基于将参数传递给模板来动态呈现HTML页面。 安装Express 首先,使用N