npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@idagio/cookie-middleware

v1.0.2

Published

Middleware for dealing with the parsing and serialization of cookies for Node.js / Express.js

Downloads

4

Readme

@IDAGIO/cookie-middleware

Middleware for dealing with the parsing and serialization of cookies for Node.js / Express.js

This middleware is very simplistic: It does exactly what it says on the tin – it gives you a way to work with cookies. What this middleware does not do:

  • asynchronous storage of data (e.g., to make data accessible to multiple servers)
  • secure storage of non-string data via use of encryption or serialization
  • session management

For the above things, you should write a module that does what you need, handling asynchronicity as it arises: don't try to make something asynchronous look synchronous.

At IDAGIO, we use two modules that deal with session management & authentication. Only one is open-source, because the other contains way too much application specific code to be made generic.

See Also

Our session management module is available as: @IDAGIO/session-middleware

Usage

As constructor:

The very basic usage of this module is to use the exported constructor on plain request and response objects. Below is an example of such:

var http = require('http');
var Cookies = require('@idagio/cookie-middleware');

var internals = {};

internals.handleRequest = function(request, response) {
  request.cookies = new Cookies(request, response);

  // Do something with request.cookies
  //
  // For instance, set a cookie: `request.cookies.set('foo', 'bar')`
  //
  // Or get a cookie: request.cookies.get('foo')

  response.writeHead(200);
  response.end('Done!');
};

http.createServer(internals.handleRequest).listen(8080);

As Express.js middleware:

Alternatively, you can use the middleware that is exported as well, for instance:

var express = require('express');
var Cookies = require('@idagio/cookie-middleware');

var app = express();

app.use(Cookies.middleware);

app.get('/', function(request, response) {
  // Use request.cookies just as above
});

app.listen(3000);

With asynchronicity:

var express = require('express');
var Cookies = require('@idagio/cookie-middleware');
var Redis = Redis.createClient(process.env.REDIS_URL);

var app = express();

app.use(Cookies.middleware);
app.use(function(request, response, next) {
  var sessionId = request.cookies.get('my_app_session');

  if (!sessionId) {
    return next();
  }

  redis.get('session:'+sessionId, function(err, data) {
    if (!err && data) {
      request.session = data;
    }

    next();
  });
});

app.get('/', function(request, response) {
  response.writeHead(200);

  if (request.session) {
    response.end('You have a session!');
  } else {
    response.end('You do not have a session :(')
  }
});

app.listen(3000);

API

new Cookies(request, response)

Creates an instance of the cookie handler using Node.js request and response objects.

This will override response.end via the js-http onHeaders module.

Cookie parsing is delegated to the js-http cookie module, however, this only happens when you use the Cookies#get method to retrieve a cookie's value.

Cookies.prototype.get(name)

Parses the cookies and returns the cookie with the given name as a String.

Cookies.prototype.set(name, value, [ options = {}, [ override=false ] ])

Sets a cookie to be sent as part of the Set-Cookie header. The options object is passed directly into the cookie modules' serialize function. For the various available options, see that module's documentation. The override parameter allows you to clear any other value that may have been set for a cookie with the given name.

Calling set multiple times with the same name will result in multiple cookies all with the same name.

Cookies.prototype.unset(name)

Shorthand for Cookies.prototype.set(name, '', { expires: new Date(0) }, true);.

Cookies.middleware(request, response, next)

Standard connect / express.js middleware. Creates an instance of Cookies stored as request.cookies.