9951 explained code solutions for 126 technologies


expressjsHow do I set a cookie using Express.js?


Using Express.js, you can set a cookie with the res.cookie() method. This method takes two parameters: the name of the cookie and an object containing the options for that cookie.

Example code

res.cookie('name', 'value', {
    maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
    httpOnly: true,
    sameSite: 'Strict'
});

This code will set a cookie named name with the value value that will expire in 7 days, is only available over HTTP, and is set with the Strict sameSite option.

Code explanation

  • res.cookie('name', 'value', { - this is the method that sets the cookie, with the name and value of the cookie as the first two parameters
  • maxAge: 1000 * 60 * 60 * 24 * 7 - this is the option that sets the cookie to expire in 7 days
  • httpOnly: true - this option sets the cookie to only be available over HTTP
  • sameSite: 'Strict' - this option sets the cookie with the Strict sameSite option

Helpful links

Edit this code on GitHub