Ben Borgers / Posts /

How to access cookies with Express

January 24, 2021 by @benborgers

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

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!

Loading...