Full Post Page
 

ExpressJS Session

Category: NODE JS & Written by Admin On May-30-2025 19:14:29

NodeJs &ExpressJs Session computer==>server=======setInfo ===Session(temporary Inforamtion) profit To Get The Inforamtion Where is Use user Authentication,shopping cart,Flash Form, Mult-Step Form,user Preferences, form Data Preservation,CAPTCHA verification A.PROCESS ExpressJs Session step 1 npm install express-session step 2 const session=require("express-session") step 3 app.use(session({ secret:'mysecretkey', resave:false, saveUninitialized:false,// true pass se session khud create hojeaga cookie:{maxAge:86400000} //1000*60*60*24 })) B. ExpressJs Session step 1 Store Session req.session.key="value"; step 2 req.send(req.session.key) srep 3 req.session.destory LAB npmjs.com search express -session https://www.npmjs.com/package/express-session npm init -y npm install express nodemon ejs npm install express-session npm i connect-mongo const Mongoose=require('connect-mongo') //store db store: Mongoose.create({ mongoUrl:'mongodb://127.0.0.1:27017/sessiondb'}), // or define Your collections name store: Mongoose.create({ mongoUrl:'mongodb://127.0.0.1:27017/sessiondb', collectionName:'mysessions'}), mongosh show dbs ,show collections db.sessions.find() db.mysessions.find() //here show Your sessions in db package.json "connect-mongo": "^5.1.0", "express": "^5.1.0", "express-session": "^1.18.1", "nodemon": "^3.1.10" app.js file /* http://localhost:3000/ output=> No user found http://localhost:3000/set-username => session created http://localhost:3000/ => show your sessoon value http://localhost:3000/get-username output show your sessoon value http://localhost:3000/destory // destroy or 24 hr auto destroy notes session created and save system Ram memory or its save in database also for step npmjss.com find connect-mongo https://www.npmjs.com/package/connect-mongo npm i connect-mongo const Mongoose=require('connect-mongo') //store db store: Mongoose.create({ mongoUrl:'mongodb://127.0.0.1:27017/sessiondb'}), // self created collections dbs mongosh show dbs ,show collections db.sessions.find() //here show Your sessions in db http://localhost:3000/destory // then deleted session from db */ //package const express=require('express'); const app=express(); const session=require('express-session') const Mongoose=require('connect-mongo') app.use(session({ secret:'secretpassword', resave:false, saveUninitialized:false,// true pass se session khud create hojeaga //store db here is self created db and collections // store: Mongoose.create({ mongoUrl:'mongodb://127.0.0.1:27017/sessiondb'}), // define Your collections name store: Mongoose.create({ mongoUrl:'mongodb://127.0.0.1:27017/sessiondb', collectionName:'mysessions'}), cookie:{maxAge:1000*60*60*24} // for 24 hour })) //route app.get('/',(req,res)=>{ if(req.session.username){ res.send(`<h2>User name from session is:${req.session.username} </h2>`) }else{ res.send(`<h2>No user Found in session:${req.session.username} </h2>`) } }); // 1.fist Set Your session then check Your any route app.get('/set-username',(req,res)=>{ req.session.username="rupeshtechnologies.com" //create session res.send("<h2>User name has been set in session </h2>") }); // 2.this route i want check session app.get('/get-username',(req,res)=>{ if(req.session.username){ res.send(`<h2>User name from session is:${req.session.username} </h2>`) }else{ res.send(`<h2>No user Found in session:${req.session.username} </h2>`) } }); //destory session // app.get('/destory',(req,res)=>{ // req.session.destroy() // }); //destory session with error app.get('/destory',(req,res)=>{ req.session.destroy((err)=>{ if(err){ res.status(500).send("failed to destory session") } res.send("<h2>Session Destory Successfully.</h2>") }) }); app.get('/about',(req,res)=>{ res.send("<h2>About Page</h2>") }); //server app.listen(3000, () => { console.log('App listening on port 3000!'); });

Comments

Share your thoughts about this post

Why Choose Us:

Experienced Instructors: Our team of experienced instructors are passionate about coding and dedicated to helping you succeed. With years of industry experience and a knack for teaching, they'll guide you every step of the way. Beginner-Friendly Content: Don't worry if you're new to coding. Our tutorials are designed with beginners in mind, starting from the fundamentals and gradually building up to more advanced topics. Updated Content: Coding is a rapidly evolving field, and we're committed to keeping our content up-to-date with the latest trends, technologies, and best practices. Community Support: Learning to code is more fun and effective when you're part of a supportive community. Join our online community forums to connect with fellow learners, share insights, and collaborate on projects. Get Started Today:



Recent Posts

Advance Js Fetch API Part-5

June-06-2025 21:23:08


Advance Js Promise Part-4

June-06-2025 11:35:05


Advance js oop part-3

June-06-2025 08:25:42


ExpressJs API With Image Crud-3

June-02-2025 21:09:41


NodeJs API-MVC-2

June-02-2025 10:56:22


ExpressJs API Crud-1

June-02-2025 10:27:58


ExpressJs Files upload

June-02-2025 10:15:40


NodeJS Cookie and CsrfToken

May-30-2025 19:20:40


ExpressJS Session

May-30-2025 19:14:29


ExpressJS NodeJs Crud Part-2

May-27-2025 21:52:18


ExpressJS NodeJS Crud Part-1

May-27-2025 21:33:30


ExpressJS Authentication

May-26-2025 17:11:41


NodeJS ExpressJS Files Upload

May-26-2025 16:53:29



Angular Introduction

May-13-2025 21:02:32



Advance Js part-1

May-12-2025 15:41:29