HTML 이스케이프는 HTML 코드에서 특수 문자를 안전하게 처리하거나 보여주기 위해 필요합니다.
웹 페이지의 구조를 파괴할 수 있는 특수 문자를 방지하거나, 사용자가 입력한 데이터를 안전하게 표시할 수 있습니다.
1. html-escaper 모듈 설치
Node.js에서 HTML 이스케이프를 쉽게 다룰 수 있게 해주는 'html-escaper' 모듈 설치
npm install html-escaper
2. html-escaper 모듈 불러오기
import 구문을 이용해 'html-escaper' 모듈 불러오기
import {escape, unescape} from 'html-escaper';
3. 이스케이프 처리하기
'html-escaper' 모듈의 'escape' 함수를 이용하여 HTML 문자열에서 특수 문자를 이스케이프 문자로 바꾸기
let str = "<div>Hello World!</div>";
let escapedStr = escape(str);
console.log(escapedStr); // <div>Hello World!</div>
4. 이스케이프 해제하기
반대로 'unescape' 함수를 이용하면 이스케이프 문자를 원래의 특수 문자로 바꾸기 가능
let str = "<div>Hello World!</div>";
let unescapedStr = unescape(str);
console.log(unescapedStr); // <div>Hello World!</div>
HTML 이스케이프는 웹 페이지의 보안과 데이터 표현의 정확성을 위해 중요한 역할을 하니 'html-escaper' 모듈을 활용하면 좋을 거 같습니다.
[참고 페이지]
반응형
'개발정리 (nodeJS)' 카테고리의 다른 글
[nodeJS] fs 모듈을 이용하여 다중 폴더 생성하기 (0) | 2023.12.30 |
---|---|
[nodeJS] node 버전 변경하기 (nvm) (0) | 2023.11.30 |
[nodeJS] Puppeteer로 간단한 검색 웹 매크로 만들기 (0) | 2023.09.09 |
[nodeJS] JWT 인증하기 (0) | 2023.08.31 |
[nodeJS] Node.JS 서버의 사용량 제한 구현하기 (0) | 2023.08.30 |