@midwayjs/express-session
v3.19.0
Published
midway session component for express
Downloads
450
Readme
@midwayjs/express-session
Session component for @midwayjs/express
Install
$ npm i @midwayjs/express-session --save
$ npm i @types/express-session --save-dev
Usage
@midwayjs/express has enabled this component by default.
We use cookie-session to keep session by default and use express-session when set custom session store.
Config
You can configure session in your config.*.ts
.
default value.
export const session = {
secret: undefined, // must be set in application
name: 'MW_SESS',
cookie: {
maxAge: 24 * 3600 * 1000, // ms
httpOnly: true,
// sameSite: null,
},
};
You can set 'session.cookie' for cookie-session config.
Custom Session Store
You can use compatible session store here.
Let's give an example for memorystore.
import { Configuration, Inject } from '@midwayjs/core';
import * as session from '@midwayjs/express-session';
import MemoryStore = require('memorystore');
@Configuration({
imports: [
express,
session,
],
//...
})
export class AutoConfiguration {
@Inject()
sessionStoreManager: session.SessionStoreManager;
async onReady() {
this.sessionStoreManager.setSessionStore(MemoryStore, {
checkPeriod: 86400000 // prune expired entries every 24h
});
}
}
Another example for connect-redis.
import { Configuration, Inject } from '@midwayjs/core';
import * as session from '@midwayjs/express-session';
import RedisStore from "connect-redis"
import {createClient} from "redis"
// Initialize client.
let redisClient = createClient()
redisClient.connect().catch(console.error)
@Configuration({
imports: [
express,
session,
],
//...
})
export class AutoConfiguration {
@Inject()
sessionStoreManager: session.SessionStoreManager;
async onReady() {
// Initialize store.
this.sessionStoreManager.setSessionStore(RedisStore, {
client: redisClient,
prefix: "myapp:",
// ...
});
}
}
Questions & Suggestions
Please open an issue here.
License
MIT