# 6회차

## 댓글구현

1. CommentModel 생성
2. PostModel의 id값을 받아서 CommentModel의 post\_id 필드로 저장
3. 댓글 작성해도 페이지 변동없도록 ajax적용

## Validation Check

웹사이트 데이터 유효성 검증 이유 : 프론트 단에서 이메일 양식에 맞는지 검증을 통과해도 브라우저에서 js끄고 form 전송시 DB에 저장 전 유효성 체크를 함 / 악의적인 데이터 삽입 방지

## XXS

* Cross Site Scripting 공격
* 글 등록시 location.href 와 같이 페이지를 이동하게 하거나 매 초마다 목표사이트를 공격하게 하는 스크립트 삽입, 또는 사이트 쿠키를 가로채고 전송
* script 삽입, iframe삽입, img&#x20;
* 해당 스크립트가 삽입된 페이지를 사용자가 방문시 스크립팅 실행

### 방어법

* DB에 저장 전 특수문자를 필터링한다.
* <, >, 을 html 특수문자 코드로 변환한다.
* 그 밖의 문자열은 지운다.
* ex)
* var html = “document”;
* 변환후
* html = < script >….
* Nodejs 의 경우 (npm install xss)

```javascript
var xss = require('xss');
var html = xss('<script>alert("xss");</script>');
console.log(html);

// 참조 : https://www.npmjs.com/package/xss
```

## CSRF

* 사용자가 자신의 의지와는 무관하게 글을 등록, 수정, 삭제 를 서버에 요청
* 2008년 옥션 해킹에도 사용된 기법
* 방어법 : 토근 발행 ( 글 작성 전 서버에서 생성한 토큰과 맞는지 확인 )

## multer

* 웹파일 전송방식중 multipart/form-data 방식을 지원해주는 모듈 (npm install –save multer)
* 적용순서 1. DB에 저장될 필드 수정(PostModel) 2. npm install –save multer 3. uploads 폴더생성 4. router 처리 ( write, edit - post ) 5. view 처리 ( detail )

```javascript
var upload = multer({ storage: storage });
multer 셋팅을 저장후

router.post(‘URL', upload.single(‘thumbnail')
// 라우터에서 적용
// thumbnail 필드명으로 파일을 받는다.
```

```javascript
// 저장 ( routes/admin ) – write (get)
var product = new ProductsModel({
name : req.body.name,
thumbnail : (req.file) ? req.file.filename : "",
price : req.body.price,
description : req.body.description,
});

// 파일업로드시 파일이 존재하면 파일명을 thumbnail 필드에 저장
```


---

# 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://incheol-jung.gitbook.io/docs/study/nodejs/2018-01-16-nodejs-6st.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.
