Hello folks! Welcome back to a new edition of our tutorial on ExpressJS. In this tutorial guide, we will be discussing about Cookies in ExpressJS.
What are Cookies?
Cookies are simple, small files/data that are sent to the client with a server request and stored on the client side. Everytime the user loads the website back, the cookie is sent with the request. This helps us to keep track of the user's actions.
Uses of Cookies
The following are the numerous uses of the HTTP Cookies -
- Session management
- Personalization
- User tracking
To utilize cookies with Express, we need the cookie-parser middleware. To install it, use the following code -
npm install --save cookie-parser
Now in order to utilize cookies with Express, we'll need the cookie-parser. cookie-parser is a middleware used to parse cookies that are attached to the client request object. To use it, we will need it in our index.js file; this can be used the same way as we use other middleware. Here, we're going to make use of the following code -
var cookieParser = require('cookie-parser'); app.use(cookieParser());
The cookie-parser parses cookie header and populates req.cookies with an object keyed by the cookie names. To set a new cookie, let's define a new route in your Express app like this -
var express = require('express'); var app = express(); app.get('/', function(req, res){ res.cookie('name', 'express').send('cookie set'); //Sets name = express }); app.listen(3000);
To check if your cookie is set or not, simply go to your browser, fire up the console, and enter -
console.log(document.cookie);
READ: Express.js | Database
You are going to get the response such as (you may have more cookies set maybe due to extensions in your browser) -
"name = express"
The browser also sends back cookies every time it queries the server. To view cookies from your server, on the server console in a route, add the following code to that route.
console.log('Cookies: ', req.cookies);
Next time you send a request to this route, you will get the following output -
Cookies: { name: 'express' }
Adding Cookies with Expiration Time
You can add cookies that expires. To add a cookie that expires, just pass an object with the property expire set to the time when you want it to expire. For example,
//Expires after 360000 ms from the time it is set. res.cookie(name, 'value', {expire: 360000 + Date.now()});
Another way to set expiration time is making use of maxAge property. Making use of this property, we can provide relative time rather than absolute time.
Example
Following is an example of this method.
//This cookie also expires after 360000 ms from the time it is set. res.cookie(name, 'value', {maxAge: 360000});
Deleting Existing Cookies
In order to delete a cookie, make use of the clearCookie function.
Example
If you need to clear a cookie called foo, use the following code -
var express = require('express'); var app = express(); app.get('/clear_cookie_foo', function(req, res){ res.clearCookie('foo'); res.send('cookie foo cleared'); }); app.listen(3000);
Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will see how to use cookies to manage sessions.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.