Set cookie value from Server Side
Table of contents
Introduction:
In the previous post, we saw how to set a cookie using javascript. In the current post, we will see how we can set cookies from the server side.
Servide Side
In order to set the cookie from the server side, we need to use the set-cookie header in the response.
The Set-cookie response header from the server directs the client to set a cookie on that particular domain. The implementation to actually create and store the cookie lies in the browser. For subsequent requests to the same domain, the browser automatically sets the Cookie request header for each request, thereby letting the server have some state to an otherwise stateless HTTP protocol. The browser uses the Domain and Path cookie attributes to determine which cookies are to be sent to a server. The server only receives name=value pairs, and nothing more.
Explanation,
We shall create a node file to have a better understanding,
const app = require("express")();
app.get("/", (req, res) => {
res.setHeader("set-cookie", ["sample-cookie=1"]);
res.send('Hello World');
});
app.listen(8080, ()=>console.log('listening...'));
Now, lets run this node app,
Let's check whether we have any cookies stored in browsers,
Now we can hit the node app, localhost:8080/
On the initial hit, we can see the cookie is from the response headers also no details of cookies are mentioned in the request header.
But on refresh, you will be able to see the cookie will be sent in request headers also,
This cookie value will be stored in the browser itself,
Final Thoughts
In this blog post, we just saw how to create a cookie from the server-side. In the upcoming post, we will see cookies with more details on their types and properties.
Subscribe to my newsletter
Read articles from Syed Jafer K directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by