kodex-music-catalog/api/v1/index.js
2023-10-01 22:31:26 +03:00

58 lines
1.3 KiB
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("./response-wrapper");
// const config = require('../../config-handler');
// Парсинг куки
//router.use(require('cookie-parser')());
// Загрузка музыки при помощи спец. метода
function muzicLoad(req, res) {
res.setHeader("Content-Type", "audio/mpeg");
global.database.muzic.get(
(err, data) => {
data = data[0]?.data;
if (err) {
res.send(Buffer.from([]));
} else {
res.send(!data ? Buffer.from([]) : data);
}
},
{ where: { id: !Number.isInteger(+req.query.id) ? 0 : +req.query.id } },
);
}
// Подгрузка с файла
router.use("/:method_name", async (req, res, next, ...etc) => {
if (req.params.method_name === "muzic") {
muzicLoad(req, res);
return;
}
try {
const methodFunct = require(`./methods/${req.params.method_name}`);
response(methodFunct, req, res);
} catch (e) {
if (e.message.includes("Cannot find module")) {
const ApiError = require("./errorClass");
res.status(400).sendModed(
await response(
(req, res) => {
throw new ApiError("METHOD_NOT_FOUNDED");
},
req,
res,
),
);
} else {
response(
async () => {
throw e;
},
req,
res,
);
}
}
});
module.exports = router;