README
@aex/session
Generic Node.js Session
This package is created for Aex, but it is useful for Node.js http
callback.
Install
npm install @aex/session
# or
yarn add @aex/session
Usage
MemoryStore
import { MemoryStore, Cookie } from "@aex/session";
import * as http from "http";
const store = new MemoryStore();
const cookie = new Cookie(store);
const scope: any = {};
const server = http
.createServer(function (req: any, res: any) {
cookie.parse(req, res, scope).then(() => {
scope.session.user = "alice";
res.write("Hello World!");
res.end();
});
})
.listen(port);
RedisStore
RedisStore uses node-redis
and takes exactly what createClient
takes which described here;
import { RedisStore, Cookie } from "@aex/session";
import * as http from "http";
const store = new RedisStore();
const cookie = new Cookie(store);
const scope: any = {};
const server = http
.createServer(function (req: any, res: any) {
cookie.parse(req, res, scope).then(() => {
scope.session.user = "alice";
res.write("Hello World!");
res.end();
});
})
.listen(port);
scope
is optional. If scope is not provided, session will be attached toreq
, make sure session is request specific even scope can be global.