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

isomorphic-cookie

v1.2.4

Published

Load and save cookies on the client and server

Downloads

48,028

Readme

isomorphic-cookie

Load, save and remove cookies within your isomorphic application

The initial idea for this package came from the popular react-cookie package, but we've made it a little harder to make serious mistakes using plugToRequest() with async server operations. Instead of providing that function, that allows you to only read/write to one request at a time (the current plugged request), we've allowed for passing the request to the load() function and the response to the save() and remove() functions. This way it's easier to reason about exactly which request/response you're interacting with.

This currently supports Express and Hapi servers. If you need to support another server framework, feel free to send a PR or make a feature request in issues.

Download

npm install --save isomorphic-cookie

Usage

isomorphicCookie.load(name, [request], [doNotParse])

isomorphicCookie.save(name, val, [options], [response])

isomorphicCookie.remove(name, [options], [response])

options (object)

path

cookie path

expires

absolute expiration date for the cookie (Date object)

domain

domain for the cookie

secure

only allow on https connections, defaults to true, you must set to false to use over http

httpOnly

** NOTE: You must set { secure: false } to use this over http. So if you're just trying it out and testing locally, make sure to set that option. If you do local development then deploy to an https server, make it conditional based on environment.

Examples

On the client:

const isomorphicCookie = require('isomorphic-cookie');

console.log(isomorphicCookie.load('serverCookie'));
console.log(isomorphicCookie.load('clientCookie'));

isomorphicCookie.save('clientCookie', 'Cookie value here');

On the server (Express):

const express = require('express');
const cookieParser = require('cookie-parser');
const path = require('path');

const isomorphicCookie = require('isomorphic-cookie');

const app = express();

app.use(express.static('dist'));
app.use(cookieParser());

app.get('/', (req, res) => {
  console.log(`client cookie: ${isomorphicCookie.load('clientCookie', req)}`);
  console.log(`server cookie: ${isomorphicCookie.load('serverCookie', req)}`);

  isomorphicCookie.save('serverCookie', 'Cookie value here', {}, res);

  res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(8000);

On the server (Hapi):

'use strict';

const Hapi = require('hapi');
const path = require('path');
const isomorphicCookie = require('isomorphic-cookie');

// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
  host: 'localhost',
  port: 8000,
});

let callIndex = 0;

server.register(require('inert'), (err) => {
  if (err) {
    throw err;
  }

  server.route({
    method: 'GET',
    path:'/',
    handler: (request, reply) => {
      callIndex++;

      console.log(`client cookie: ${isomorphicCookie.load('clientCookie', request)}`);
      console.log(`server cookie: ${isomorphicCookie.load('serverCookie', request)}`);

      isomorphicCookie.save('serverCookie', 'Cookie value here', {}, reply);

      reply.file(path.join(__dirname, 'index.html'));
    },
  });

  server.start((err) => {
    if (err) {
      throw err;
    }
    console.log('Server running at:', server.info.uri);
  });
});

License

This project is under the MIT license. You are free to do whatever you want with it.