Node.js ‘he’ 패키지: HTML 엔티티 인코딩 및 디코딩 방법
이 글에서는 Node.js의 ‘he’ 패키지를 사용하여 HTML 엔티티를 인코딩하고 디코딩하는 방법에 대해 알아봅니다. 다양한 예제를 통해 ‘he’ 패키지의 기본 사용법을 쉽게 이해할 수 있습니다.
he 패키지 설치
먼저 ‘he’ 패키지를 설치해야 합니다. 다음 명령어를 터미널에 입력하여 설치합니다.
npm install he
HTML 엔티티 인코딩
‘he’ 패키지를 사용하여 문자열을 HTML 엔티티로 인코딩하는 방법입니다.
const he = require('he');
const htmlString = '<div>Hello, World!</div>';
const encodedString = he.encode(htmlString);
console.log(encodedString); // <div>Hello, World!</div>
HTML 엔티티 디코딩
‘he’ 패키지를 사용하여 HTML 엔티티를 디코딩하는 방법입니다.
const he = require('he');
const encodedString = '<div>Hello, World!</div>';
const decodedString = he.decode(encodedString);
console.log(decodedString); // <div>Hello, World!</div>
▼ HTML 엔티티 인코딩 및 디코딩 예제 결과

참고할 만한 사이트
관련 이전 게시글
- Node.js에서 Nodemailer를 사용한 이메일 인증 구현 예제
- Node.js에서 dotenv 라이브러리를 활용한 환경 변수 설정하는 방법
- Node.js의 Express와 Cookie-Parser로 쿠키를 활용하는 방법
- Node.js에서 비동기 처리: Callback, Promise, async/await 사용법
- Express 애플리케이션에서 사용자 정의 미들웨어 만드는 방법
이 글이 도움이 되셨다면 공유 부탁 드립니다.