Using cookies to store data
Cookies are small bits of information that a website can store in a user's browser. They may be used for various purposes, from providing users with a customized and lively experience to surreptitiously tracking their browsing habits.
Cookies can be created by the server backend or the frontend JavaScript. They can then be read or updated by either one of them.
Note
Install a cookie-editing extension such as Edit This Cookie in Google Chrome to interactively experiment with the cookies created in your browser.
Express provides a cookie API using the cookieParser
middleware. To enable the cookie functionality in Express, load it before the router
middleware:
app.use(express.cookieParser());
With this middleware enabled, you can find the cookies sent by the browser in the req.cookies
object, and set cookies using the res.cookie()
method.
Note
The cookieParser
middleware must be loaded before the router
middleware; else cookie functionality will not be enabled.
That was a...