Follow

Follow
Set cookie value from Server Side

Photo by Sara Sperry on Unsplash

Set cookie value from Server Side

Syed Jafer K's photo
Syed Jafer K
·Oct 3, 2022·

2 min read

Play this article

Table of contents

  • Introduction:
  • Servide Side
  • Explanation,
  • Final Thoughts

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,

image.png

Let's check whether we have any cookies stored in browsers,

Screenshot from 2022-09-20 19-04-44.png

Now we can hit the node app, localhost:8080/

Screenshot from 2022-09-20 19-05-34.png

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,

Screenshot from 2022-09-20 19-06-19.png

This cookie value will be stored in the browser itself,

image.png

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.

Did you find this article valuable?

Support Make Reading by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this