ZL
About Articles Contact
Published on Mar 20, 2022
Filed under:
#node,
#express

Getting a cookie's expiry value on a server

Browsers handle cookie expiry so they don’t pass the cookie’s expiry value to the server. You have to make some adjustments if you want to get the cookie’s expiry value on the server.

There are two methods:

  • You can create a cookie with a JSON value
  • You can use another cookie to signify the expiry

Creating a cookie with a JSON value

You can create a cookie with a JSON value. It looks like this:

const cookieValue = JSON.stringify({
value: 'hello world',
expiry: Date.now() + 3600 * 1000,
})
res.cookie('myCookie', cookieValue)

If you use Express, you can indicate to cookie-parser that a cookie is a JSON cookie by prepending the value with a j:. Cookie-parser will automatically decode the JSON cookie and turn it back into an object.

// Setting a JSON cookie for cookie-parser
const cookieValue = JSON.stringify({
value: 'hello world',
expiry: Date.now() + 3600 * 1000,
})
res.cookie('myCookie', `j: ${cookieValue}`)
// Reading the JSON cookie
import cookieParser from 'cookie-parser'
app.use(cookieParser())
app.get('/', (req, res) => {
const { myCookie } = req.cookies
if (myCookie.expiry < Date.now()) {
// Do something
}
})

Of course, if you want the browser to remove the cookie automatically when it expires, you can still set the maxAge property.

res.cookie('myCookie', 'j:' + cookieValue, { maxAge: 3600 * 1000 })

Creating another cookie to store the expiry

You can create another cookie to store the expiry value. Here’s what it looks like (including the maxAge property).

res.cookie('myCookie', 'hello world', { maxAge: 3600 * 1000 })
res.cookie('myCookieExpiry', Date.now() + 3600 * 1000 { maxAge: 3600 * 1000 })

You’d be able to check the cookie expiry value in the server like this:

import cookieParser from 'cookie-parser'
app.use(cookieParser())
app.get('/', (req, res) => {
const { myCookie, myCookieExpiry } = req.cookies
if (myCookieExpiry < Date.now()) {
// Do something
}
})

That’s it!

Previous Rsync with a custom port Next Don't be ashamed of tutorial hell

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is one of those rare people who commands tremendous knowledge and experience but remains humble and helpful. They want you to know what they know, not just be impressed by it.

In other words, Zell is a natural teacher. You’re lucky to have him because he feels lucky to be able to help you in your journey.

Heydon Pickering
Heydon Pickering — Web & Accessibility Extraordinaire
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

youtube instagram tiktok github bluesky twitter

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms