# 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**,&#x20;

```javascript
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씩 증가한다.**

```javascript
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);
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/web-application/cookie/shopping-cart-page.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
