Session store

PussinSocks·2022년 7월 26일
0

CloneCoding

목록 보기
16/20

Problem with session

Session is created when a browser first reachs to a backend. And browser only gets the sessionID. Problem is that the default server-side session storage is a MoemoryStore. Means it is volatile. It will perish whenever the backend restart or the server dies. (Not designed for a product environment)
more info


connect-mongo

Saves the session in the mongoDB.

  1. Install connect-mongo package through terminal
    npm i connect-mongo
  2. Import MongoStore in the server.js
    import MongoStore from "connect-mongo";
  3. Write command lines on the app.use(session)
app.use(session({
    secret: "Hello",
    resave: true,
    saveUninitialized: true,
    store: MongoStore.create({mongoUrl: "mongodb://127.0.0.1:27017/youtubeclone"}),
})
);
  • This creates a sessions db in our MongoDB.
  • When a browser interact with the backend, the sessionID given to the browser will be stored in the db.
profile
On a journey to be a front-end developer

0개의 댓글