✍️
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

Session with Express

세션방식으로 count 페이지(count값이 1씩 증가하는 페이지)를 만들어보겠다. 코드 설명 : req.session객체를 통해 'count'라는 데이터를 심고, 값은 1로 셋팅한다.

app.get('/count',(req,res)=>{
     req.session.count = 1;
     res.send('hi session');
 });

sid(session id)로 접속한 사용자는 서버에 count라는 값으로 '1'이 저장되어 있고, 이 값은 우리가 직접 req.session.count로 저장할 수도 있지만, req.session.count로 값을 읽어올 수도 있다. req.session.count는 일종의 쿠키 데이터인듯하다. session의 핵심은, sid별로 별도의 데이터를 서버에 저장할 수 있다는 것이다..

express-session 이라는 모듈은 기본적으로, 사용자의 정보를 메모리에 저장한다. 그렇기 때문에 해당 서버(어플리케이션)을 껐다 켜면 정보는 날아간다.

모듈명 사용할 때 주의하기! 'express-session' 을 'session'이라고 해서 터미널에서 'session'이라는 모듈을 못 찾아 무한루프(?)에 빠지게 되어 맥북을 몇 번 껐다 켰는지 모른다..터미널 셋팅하는 방법도 헤매서.. 결론 : 모듈명을 정확하게 기술하고, 터미널 다시 새로 살리는 방법 익히기..!

PreviousSession and CookieNextLogin function using session

Last updated 4 years ago

Was this helpful?