How to access cookies with Express

March 18, 2020

The best way to access cookies with an Express server is using the cookie-parser middleware package.

Install it:

npm install cookie-parser

And then add it to your server:

const express = require("express");
const app = express();

const cookieParser = require("cookie-parser");
app.use(cookieParser());

// the rest of your Express server...

You can then access the cookies of any request:

app.get("/", (req, res) => {
  const cookies = req.cookies;
  const token = req.cookies.token;

  // ...etc
});

Get new posts by email (or RSS)!