kodex-music-catalog/api/v1/index.js
2023-09-30 04:14:59 +03:00

29 lines
813 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const router = require('express').Router();
const response = require('./responseDecorator');
// const config = require('../../config-handler');
// Подгрузка с файла
function catchError (func) {
func();
}
router.use('/:method_name', async (req, res, next, ...etc) => {
try {
const methodFunct = require(`./methods/${req.params.method_name}`);
response(methodFunct, req, res);
}
catch (e) {
try {
console.log(e);
const ApiError = require('./errorClass');
catchError(
() => res.status(400).send(response((req, res) => {
throw new ApiError("METHOD_NOT_FOUNDED");
}, req, res))
);
}
catch (_e) {} // Посылаем 'Cannot set headers after they are sent to the client' в.. обработку.. без обработчика.
}
});
module.exports = router;