express-session-cloudflare-kv
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

express-session-cloudflare-kv

A cloudflare (workers) KV based express session store.

This session store use Cloudflare API from Node.js application.

Install

Install with npm:

npm install express-session-cloudflare-kv

You also need to install express:

npm install express express-session

Usage

Requirements:

const express = require("express");
const session = require("express-session");
const { createSessionStore } = require("express-session-cloudflare-kv");
const app = express();
app.use(session({
    store: createSession({
        // cloudflare account id
        accountId: process.env.CF_accountId,
        // cloudflare KV namespace **id** (not name)
        namespaceId: process.env.CF_namespaceId,
        // cloudflare account email
        authEmail: process.env.CF_authEmail,
        // cloudflare API Key(probabely require Global Key)
        authKey: process.env.CF_authKey
    }),
    secret: "xxx",
    resave: false,
    saveUninitialized: true
}));
// Access the session as req.session
app.get('/', function(req, res, next) {
    console.log("req.session.id", req.session.id);
    if (req.session.views) {
        req.session.views++
        res.setHeader('Content-Type', 'text/html')
        res.write('<p>views: ' + req.session.views + '</p>')
        res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
        res.end()
    } else {
        req.session.views = 1
        res.end('welcome to the session demo. refresh!')
    }
})

app.listen(8000, () => {
    console.log("http://localhost:8000");
});

Options

export type CreateSessionOptions = {
    // cloudflare account id
    accountId: string;
    // KV storage namespace Id
    namespaceId: string;
    // cloudflare account email
    authEmail: string;
    // cloudflare Global API Key
    authKey: string;
    // https://developers.cloudflare.com/workers/runtime-apis/kv#expiring-keys
    // Set its "expiration", using an absolute time specified in a number of seconds since the UNIX epoch
    expiration?: number;
    // Set its "expiration TTL" (time to live), using a relative number of seconds from the current time. For example, if you wanted a key to expire 10 minutes after creating it, you would set its expiration TTL to 600.
    expirationTtl?: number;
};

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Package Sidebar

Install

npm i express-session-cloudflare-kv

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

25.9 kB

Total Files

17

Last publish

Collaborators

  • azu