✍️
Web Application
  • Cookie
  • Intro
    • HTTP
  • Cookie
    • Counting Cookie
    • Shopping Cart Using Cookie
    • Shopping Cart Page:id
    • Shopping Cart Page
    • Cookie and Security
  • Session
    • Intro
    • Session and Cookie
    • Session with Express
    • Login function using session
    • How to store session data
    • store as file
    • store in mysql DB
    • Store Session in OrientDB
  • Security Password
    • salt
    • PBKDF2
    • Log in with encrypted password
    • PassportJS
    • Untitled
  • Federation Authentication
    • Intro
    • Facebook Authentication
    • FB Auth Summary
  • refactoring
    • template engine : pug extends, include
    • Modularity
    • Divide Router
Powered by GitBook
On this page

Was this helpful?

  1. Session

store as file

파일에 세션 데이터를 저장하는 방법

session은 메모리에 저장되고, 재접속 시에 데이터가 저장되지 않으므로 일반적으로 잘 쓰지 않는다고 한다. 보통 파일이나 DB에 저장한다.

  • 파일에 세션 데이터를 저장하는 방법 express-session은 사용자의 세션 데이터를 store옵션이 가리키고 있는 파일에 저장한다.(FileStore()). store:new FileStore()는 자동으로 'session'이라는 디렉토리를 생성하고 여기에 세션데이터를 저장한다.

var FileStore = require('session-file-store')(session);
app.use(session({
    //secret이란 사용자의 웹 브라우저에 sid(session id)를 심을 때 평문으로 심으면 위험하기 때문에
    //resave-false : 서버에 접속할 때마다 새로운  sid를 발급받지 않겠다.
    //세션을 실제로 사용하기 전까지는 발급하지 말아라.
    secret: 'GOFORHLS!HLS!HLS!',
    resave: false,
    saveUninitialized: true,
    store:new FileStore()//session 데이터를 저장할 'session'이라는 디렉토리를 생성한다!
  }));
  • 페이지에 접속하면 session 디렉토리가 생성되고, json파일이 생성되며 세션 데이터가 저장된다.

  • 로그인하면 이 json 파일에 displayName이 추가된다.

  • 접속하던 세션을 삭제하고 새로운 세션에서 로그인하면 새로운 세션에 대한 새로운 json 파일이 새로 생성된다.

PreviousHow to store session dataNextstore in mysql DB

Last updated 4 years ago

Was this helpful?