Node JS Tutorial in Hindi #61 Set and Get Cookies in Node js

Set and Get Cookies in Node js

Points of video

What are Cookies?

​Make html form.

Set Cookies.

Check Cookies are stored or not.

Display Cookies data on UI

Interview Questions

Notes, Code and playlist.


what is cookies ?

Cookies are small pieces of data that a server sends to the client (browser),

Which the browser stores and sends back to the server with each subsequent request.


Code of index.js file


import express from 'express'

const app = express();


app.set("view engine",'ejs')
app.use(express.urlencoded({extended:true}))
app.get("/login",(req,resp)=>{
resp.render('login')
})


app.post("/profile",(req,resp)=>{
resp.setHeader('Set-Cookie',"login=true")
resp.setHeader('Set-Cookie',"name="+req.body.name)

resp.render('profile')
})

app.get("/",(req,resp)=>{
let cookiesData= req.get('cookie');

cookiesData= cookiesData.split(";")

cookiesData= cookiesData[1].split("=");

console.log(cookiesData[1]);
resp.render('home',{name:cookiesData[1]})
})

app.listen(3200)


Github Link

https://github.com/anil-sidhu/node-express-mongodb/blob/cookies/index.js