What is cookies in node express.

What is cookies in node express.

Β·

2 min read

what?.

Small file of information that is stored within the user's computer. Cookies generally consist of Name, value, and key-value format.

Use?

suppose you are visiting a website and the website is in another language. So you need to change it with your language and you change the language setting and now you read your content. But wait what happens when you revisit the same website? Will it require changing the language setting again or something else? Here cookie helps when you first time visited the website it stores the language setting in your browser so the next time you visit the same website then it will not ask to change the setting it will automatically change the language from your cookies.

let’s Bake These Cookies!!πŸͺπŸͺ

First of all, create a simple express server

Step 1

install NPM with the command

 npm init -y

Step 2

install express.

 npm i express

Step 3

Cookies!!πŸͺπŸͺ Time

npm i cookie-parser

Let's code.

const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser());

app.get('/',(req,res,next)=>{
  res.send('Hello cookie')
});

const port = 3000;
app.listen(port,()=>{
    console.log(`server is runing on loalhost ${port}`)
})

In the above code, we created a local server and add the cookieParser dependencies.

In Brower

In the cookie section, nothing is there.

Cookie code

const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser);


app.get('/',(req,res,next)=>{
  res.send('Hello cookie')
});

app.get('/set_cookie',(req,res,next)=>{
    res.cookie('soo','bar');
    res.send('cookie is set');
})

const port = 3000;
app.listen(port,()=>{
    console.log(`server is runing on loalhost ${port}`)
})

For deleting the set cookie we just use clearCookie.

// Deleting cookies..
app.get('/del_cookie',(req,res,next)=>{
    res.clearCookie('soo');
    res.send('Cookie is has been deleted');
})

Overall code.

const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();

app.use(cookieParser());

app.get('/',(req,res,next)=>{
  res.send('Hello cookie')
});

app.get('/set_cookie',(req,res,next)=>{
    res.cookie('soo','bar');

    res.send('cookie is set');
})

app.get('/del_cookie',(req,res,next)=>{
    res.clearCookie('soo');
    res.send('Cookie is has been deleted');
})

const port = 3000;

app.listen(port,()=>{
    console.log(`Server is runing on loalhost ${port}`)
})

a cookie can take object inside so you can add more things inside it such as expiration date https security, Maxage etc.

resp.cookie('foo','bar',{

    // age of cookie
    // maxAge:5000,
    // expires:new Date('23 July 2023')
    // httpOnly:true,
    secure:true,
  });

Thank you for reading my content. Be sure to follow and comment on what you want me to write about next :)

Did you find this article valuable?

Support Saurabh verma by becoming a sponsor. Any amount is appreciated!

Β