Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cookie-session

Package Overview
Dependencies
Maintainers
6
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cookie-session

cookie session middleware

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
195K
increased by5.16%
Maintainers
6
Weekly downloads
 
Created

What is cookie-session?

The cookie-session npm package is a simple middleware for managing session data using cookies. It stores session data on the client within a cookie, making it stateless and lightweight. This package is particularly useful for small to medium-sized applications where server-side session storage is not necessary.

What are cookie-session's main functionalities?

Basic Session Management

This code demonstrates basic session management using cookie-session. It sets up an Express application with cookie-session middleware, which stores session data in a cookie. The session data includes the number of views for the root URL.

const express = require('express');
const cookieSession = require('cookie-session');

const app = express();

app.use(cookieSession({
  name: 'session',
  keys: ['key1', 'key2'],
  maxAge: 24 * 60 * 60 * 1000 // 24 hours
}));

app.get('/', (req, res) => {
  req.session.views = (req.session.views || 0) + 1;
  res.send(`Number of views: ${req.session.views}`);
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Custom Cookie Options

This code demonstrates how to customize cookie options with cookie-session. It includes options like httpOnly and secure to enhance security by ensuring the cookie is only accessible via HTTP(S) and only used over HTTPS.

const express = require('express');
const cookieSession = require('cookie-session');

const app = express();

app.use(cookieSession({
  name: 'session',
  keys: ['key1', 'key2'],
  maxAge: 24 * 60 * 60 * 1000, // 24 hours
  httpOnly: true, // Ensures the cookie is only accessible via HTTP(S), not JavaScript
  secure: true // Ensures the cookie is only used over HTTPS
}));

app.get('/', (req, res) => {
  req.session.views = (req.session.views || 0) + 1;
  res.send(`Number of views: ${req.session.views}`);
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Clearing Session Data

This code demonstrates how to clear session data using cookie-session. By setting req.session to null, the session data is cleared, effectively logging the user out.

const express = require('express');
const cookieSession = require('cookie-session');

const app = express();

app.use(cookieSession({
  name: 'session',
  keys: ['key1', 'key2'],
  maxAge: 24 * 60 * 60 * 1000 // 24 hours
}));

app.get('/logout', (req, res) => {
  req.session = null; // Clears the session data
  res.send('Logged out');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Other packages similar to cookie-session

Keywords

FAQs

Package last updated on 07 May 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc