Ben Borgers

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
})

More blog posts

Subscribe to my newsletter for a monthly round-up of new blog posts and projects Iā€™m working on!

Twitter ↗ RSS feed ↗