Node JS Tutorial in Hindi #62 - Session

Set and Get Session in Node js

Points of this video

  1. What are Session?
  2. ​Install Session npm package
  3. Set Session.
  4. Check Session are stored or not.
  5. Display Session data on UI
  6. Interview Questions
  7. Notes, Code and playlist.


What is Session?

  1. Store Data on server
  2. More secure
  3. Key stored in cookies


index.js file code


import express from 'express'
import session from 'express-session';

const app = express();

app.set("view engine", 'ejs')

app.use(session({
secret:'apple',
}))


app.use(express.urlencoded({ extended: true }))


app.get("/login", (req, resp) => {
resp.render('login')
})

app.post("/profile",(req,reps)=>{
req.session.data= req.body;
console.log(req.session.data);
reps.render('profile')
})

app.get("/",(req,resp)=>{
const data = req.session.data;
console.log("data",data);
resp.render("home",{data})
})




app.listen(3200)


Code Link:- https://github.com/anil-sidhu/node-express-mongodb/tree/session