Как обрабатывать ошибки в async Express роутах?

Middle
331 просмотров
AFK Offer AI

В Express 4 async ошибки нужно явно передавать в next(). Паттерн: router.get("/", async (req, res, next) => { try { const data = await service.getData(); res.json(data); } catch(err) { next(err); } }). Утилита asyncHandler: const wrap = fn => (req, res, next) => fn(req, res, next).catch(next). Применяешь: router.get("/", asyncHandler(handler)). Express 5 (в beta) делает это автоматически. express-async-errors пакет патчит Express 4 для автоматического проброса.

Следующий вопрос

Как реализовать RBAC (Role-Based Access Control) в Node.js?