Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Using client-sessions without Connect/Express #110

Open
ohenepee opened this issue Jan 31, 2016 · 1 comment
Open

Using client-sessions without Connect/Express #110

ohenepee opened this issue Jan 31, 2016 · 1 comment

Comments

@ohenepee
Copy link

Please can client-sessions be used with Raw HTTP(s)?

@ottiker
Copy link

ottiker commented Jun 27, 2016

Hi! We use this module without connect. See here for an example.

But basically you can just pass the req and res objects from node's http.createServer to the client session handler.

var sessions = require("client-sessions");
var http = require('http');

var requestSessionHandler = sessions({
    cookieName: 'mySession', // cookie name dictates the key name added to the request object
    secret: 'blargadeeblargblarg', // should be a large unguessable string
    duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
    activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
});

http.createServer(function (req, res) {
    requestSessionHandler(req, res, function () {
        if (req.mySession.seenyou) {
            res.setHeader('X-Seen-You', 'true');
        } else {
            // setting a property will automatically cause a Set-Cookie response
            // to be sent
            req.mySession.seenyou = true;
            res.setHeader('X-Seen-You', 'false');
        }
    });
}).listen(8000); 

I didn't test this example, but it should show you how it can be done.

..maybe there are better ways to use client-sessions without connect?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants