본문 바로가기
IT/web

Node.js] 쿠키 설정, 세션 설정

by 깻잎쌈 2020. 1. 24.
반응형

사용자가 로그인한 상태인지 확인할 때 사용한다.

쿠키는 클라이언트 웹에 저장되는 정보이고, 세션은 웹 서버에 저장된다.


 쿠키

 

cookie-parser

Parse HTTP request cookies

www.npmjs.com

 

쿠키 설정할 때는 cookie, 쿠키를 읽어올 때는(가져올때는) cookies

 

How to set cookie in node js using express framework?

In my application, I need to set a cookie using the express framework.I have tried the following code but it's not setting the cookie. Can anyone help me to do this? var express = require('expres...

stackoverflow.com

 

router.route('/process/showCookie').get(function(req,res){
    console.log('/process/showcookie 호출됨');
    
    //쿠키 화면에 표시
    res.send(req.cookies);
});

router.route('/process/setUserCookie').get(function(req,res){
    console.log('/process/setUserCookie 호춛됨 ');
    
    // 쿠키설정 
    res.cookie('user', {
        id: 'mimi',
        name: '구준희',
        authorized: true
    });
    
   res.redirect('/process/showCookie');
});

 


세션

 

express-session

Simple session middleware for Express

www.npmjs.com

user 세션 저장

req.session.user = {
 id:paramId,
 name: '구준희',
 authorized:true
 };

세션 삭제

req.session.destroy(funtion(err){
	if(err) {throw err;}
    
    console.log('세션삭제했고, 로그아웃되었음');
    res.redirect('/public/login2.html');
    });

 

개발자 도구에서 쿠키 확인 가능 

반응형

댓글