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

Shopping Cart Page:id

From 'Product' page, once clikc the 'add' button, it will go to 'cart' page. According to product number(id), it will go to corresponding page.

It can be done by using parameter which is 'req.params.id'.

ex) If you click product1>add,

app.get('/cart/:id',(req,res)=>{
    res.send('hi');
});

Design cart, cart page

Product #id : The Number of product #id

The number of product#id means cookie.

소스코드 설명 : 쿠키가 증가하는 원리 최초 방문시 쿠키에 'cart'라는 데이터가 없으면 cart라는 객체를 생성한다. 최초 방문시 cart[id]에 값을 0으로 할당한다. 재방문 시, cart[id] 값을 1씩 증가한다.

app.get('/cart/:id',(req,res)=>{
    //cart라는 객체를 만들어서 쿠키에 'cart'라는 객체 데이터를 심을 것이다.
    var id = req.params.id;
    if(req.cookies.cart) {//cart라는 값이 있으면 이 값을 그대로 사용한다.
        var cart = req.cookies.cart;
    } else {
        var cart = {};//최초 방문시 빈 객체를 할당.
    }
    if(!cart[id]){//최초 방문시 cart[id]를 0으로 셋팅.
        cart[id] = 0;
    }
    cart[id] = parseInt(cart[id])+1;//url에서 끝의 'id'와 쿠키값을 문자->숫자로 가져온다.
    res.cookie('cart',cart);//이 cart에는 제품번호와 수량이 담겨있다! 수량=쿠키
    res.redirect('/cart');
    //res.send(cart);
});

PreviousShopping Cart Using CookieNextShopping Cart Page

Last updated 4 years ago

Was this helpful?