# Counting Cookie

**웹 브라우저가 웹 서버에 요청을 보낼 때** 쿠키를 'count=1'(**웹서버가 웹 브라우저에 이전에 보냈던 정보**) **전송**하고 있는 것을 알 수 있다.\
웹 서버는 **Set-Cookie로** 웹 브라우저로부터 받은 쿠키값에 1을 더해서 쿠키값을 셋팅한다.\
Nodejs를 이용하여 웹 브라우저와 웹 서버 사이에 통신하면서 쿠키 정보를 1씩 증가시킬 수 있다!

1. express 모듈 불러온다.
2. 포트 번호 설정.&#x20;

```javascript
app.listen(3000, function(){
    console.log('Connected 3000 port!');
});
```

3\. count페이지에 대한 라우팅 작성.

```javascript
app.get('/count',(req,res)=>{
    res.send('count:');
});
```

4\. cookie 모듈 설정 : req, res 객체를 통해 쿠키 데이터를 알 수 있다.

```javascript
app.use(cookieParser());
```

5\. 쿠키값 1씩 증가시키기

* 서버 => 웹 브라우저 쿠키값 셋팅(웹 브라우저가 서버에 재접속할 때 다음 쿠키값)

```javascript
res.cookie('count',100);//쿠키값을 100으로 셋팅한다.
```

* 웹 브라우저의 쿠키값(데이터) 가져올 때

```javascript
req.cookies.count
```

* request header의 값은 문자이기 때문에 숫자로 바꿔준다. - *parseInt( )*

```javascript
app.get('/count', (req,res)=>{
    if(req.cookies.count) {
        var count = parseInt(req.cookies.count);
    } else {
        var count = 0;//쿠키값 0으로 초기화 셋팅.
    }
    count = count +1;
    res.cookie('count',count);//기존의 count값에 1 더한 값을 쿠키(=다음 쿠키)로 셋팅한다.
    res.send('count:'+count);
});
```


---

# 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/counting-cookie.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.
