express-asyns-handler란?
: Express.js 경로 핸들러 내에서 비동기 작업 처리를 단순화하는 미들웨어
→ 명시적인 try/catch 블록 없이도 비동기 처리 를 작성할 수 있다.
설치법
npm install express-async-handler
코드예시
// @desc Get all contacts
// @route GET /contacts
const getAllContacts = async (req, res) => {
try{
res.status(200).send("Contacts Page");
} catch (error){
res.send(error.message);
}
});
module.exports = getAllContacts;
const asyncHandler = require("express-async-handler");
// @desc Get all contacts
// @route GET /contacts
const getAllContacts = asyncHandler(async (req, res) => {
// 전체 연락처 보기
res.status(200).send("Contacts Page");
});
💡 총정리
express-async-handler 는 Express.js의 비동기 작업에 대한 오류 처리를 단순화하고, try/catch 블록 없이 비동기 처리를 하여 코드를 읽기 쉽게 도와준당 !
'[NodeJS] 노드' 카테고리의 다른 글
| [NodeJS] 비밀번호 암호화하기-bcrypt 모듈 (0) | 2024.06.03 |
|---|---|
| RESTful API (0) | 2024.05.28 |
| 블로킹과 논블로킹 (0) | 2024.05.27 |
| [Javascript] let과 const 그리고 var (0) | 2024.05.27 |
| 버퍼와 스트림 (0) | 2024.05.23 |