http-errors

Это старая версия документа!


http-errors

Для зручного створення помилок скористаємось пакетом http-errors. Інсталюємо його командою:

npm install http-errors
 // src/controllers/students.js 

// 1. Імпортуємо функцію з бібліотеки
import createHttpError from 'http-errors';

/* Інший код файлу */

export const getStudentByIdController = async (req, res, next) => {
  const { studentId } = req.params;
  const student = await getStudentById(studentId);

  if (!student) {
    // 2. Створюємо та налаштовуємо помилку
    next(createHttpError(404, 'Student not found'));
    return;
  }

  res.json({
    status: 200,
    message: `Successfully found student with id ${studentId}!`,
    data: student,
  });
};
  • /sites/data/attic/http-errors.1717254657.txt.gz
  • Последнее изменение: 2024/06/01 15:10
  • tro