# store as file

session은 메모리에 저장되고, 재접속 시에 데이터가 저장되지 않으므로 일반적으로 잘 쓰지 않는다고 한다. 보통 파일이나 DB에 저장한다.

* 파일에 세션 데이터를 저장하는 방법\
  **express-session**은 사용자의 세션 데이터를 **store**옵션이 가리키고 있는 파일에 저장한다.(FileStore()). \
  store:new FileStore()는 자동으로 'session'이라는 디렉토리를 생성하고 여기에 세션데이터를 저장한다.

```javascript
var FileStore = require('session-file-store')(session);
app.use(session({
    //secret이란 사용자의 웹 브라우저에 sid(session id)를 심을 때 평문으로 심으면 위험하기 때문에
    //resave-false : 서버에 접속할 때마다 새로운  sid를 발급받지 않겠다.
    //세션을 실제로 사용하기 전까지는 발급하지 말아라.
    secret: 'GOFORHLS!HLS!HLS!',
    resave: false,
    saveUninitialized: true,
    store:new FileStore()//session 데이터를 저장할 'session'이라는 디렉토리를 생성한다!
  }));
```

* 페이지에 접속하면 session 디렉토리가 생성되고, json파일이 생성되며 세션 데이터가 저장된다.
* 로그인하면 이 json 파일에 displayName이 추가된다.
* 접속하던 세션을 삭제하고 새로운 세션에서 로그인하면 새로운 세션에 대한 새로운 json 파일이 새로 생성된다.


---

# 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/session/store-as-file.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.
