diff --git a/api/v1/errorClass.js b/api/v1/errorClass.js index a44c0e9..c76de8e 100644 --- a/api/v1/errorClass.js +++ b/api/v1/errorClass.js @@ -1,7 +1,8 @@ module.exports = class ApiError extends Error { - constructor(message, ...args) { + constructor(message, details={}, ...args) { super(message, ...args); this.message = message; + this.details = details; this.name = "ApiError"; } }; \ No newline at end of file diff --git a/api/v1/index.js b/api/v1/index.js index 3fe51d6..f1679a2 100644 --- a/api/v1/index.js +++ b/api/v1/index.js @@ -1,28 +1,22 @@ const router = require('express').Router(); -const response = require('./responseDecorator'); +const response = require('./response-wrapper'); // const config = require('../../config-handler'); -// Подгрузка с файла -function catchError (func) { - func(); -} +// Парсинг куки +//router.use(require('cookie-parser')()); +// Подгрузка с файла 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' в.. обработку.. без обработчика. + //console.log(e); + const ApiError = require('./errorClass'); + res.status(400).sendModed(response((req, res) => { + throw new ApiError("METHOD_NOT_FOUNDED"); + }, req, res)); } }); diff --git a/api/v1/methods/create-item.js b/api/v1/methods/create-item.js index 7d6a97e..31a974e 100644 --- a/api/v1/methods/create-item.js +++ b/api/v1/methods/create-item.js @@ -1,3 +1,19 @@ +const ApiError = require('../errorClass'); + +function checkSyntaxArgs (req, res) { + return ( + ( // Проверка поля type в json. + ) + ) +} + module.exports = (req, res) => { + if (req.json === undefined) { + // console.log(req.headers); + throw new ApiError("METHOD_MUST_BE_POST_JSON", { + request_method : req.method, + 'content-type' : !req.headers['content-type'] ? null : req.headers['content-type'] + }); + } return 'method create item'; } \ No newline at end of file diff --git a/api/v1/response-wrapper.js b/api/v1/response-wrapper.js new file mode 100644 index 0000000..acc6237 --- /dev/null +++ b/api/v1/response-wrapper.js @@ -0,0 +1,39 @@ +const unknownError = (err) => { + return { + error : 'UNKNOWN_ERROR', + details : {} + }; +} + +const unknownResponseFormat = 'UNKNOWN_RESPONSE_FORMAT'; + +function handlingError (funct, success, error) { + try { + success(funct()); + } + catch (e) { + // console.log('error', e); + error(e.name === 'ApiError' ? { + error : e.message, + details : e.details + } : unknownError(e)); + } +} + +module.exports = (method, req, res) => { + if (req.query.response_format === 'json') { + handlingError( + () => ({ response : method(req, res) }), + (data) => { + res.sendModed(data); + }, + (errBody) => { + res.errorModeOn(); + res.status(400).sendModed(errBody); + } + ); + } + else { + res.status(400).sendModed(unknownResponseFormat); + } +} \ No newline at end of file diff --git a/api/v1/responseDecorator.js b/api/v1/responseDecorator.js deleted file mode 100644 index bc82af6..0000000 --- a/api/v1/responseDecorator.js +++ /dev/null @@ -1,30 +0,0 @@ -const unknownError = { - error : 'UNKNOWN_ERROR' -} - -const unknownResponseFormat = 'UNKNOWN_RESPONSE_FORMAT'; - -function handlingError (funct, success, error) { - try { - success(funct()); - } - catch (e) { - // console.log('error', e); - error(e.name === 'ApiError' ? { - error : e.message - } : unknownError); - } -} - -module.exports = async (method, req, res) => { - if (req.query.response_format === 'json') { - handlingError( - () => ({ response : method(req, res) }), - (...args) => res.send(...args), - (...data) => res.status(400).json(...data) - ); - } - else { - res.status(400).send(unknownResponseFormat); - } -} \ No newline at end of file diff --git a/logger.js b/logger.js index 45e916f..8f56ad6 100644 --- a/logger.js +++ b/logger.js @@ -1,8 +1,7 @@ const config = require('./config-handler'); +const fs = require('fs'); function log (date, req, ip, res) { - // console.log(req); - const requestBody = !req.byteBody.toString('utf-8') ? '' : ` ~~~~~~~~~~~~~~ [REQUEST BODY] @@ -13,10 +12,15 @@ ${req.byteBody.toString('utf-8')}`; [HEADERS] ~~~~~~~~~ ${Object.entries(req.headers).map(([header, value]) => header + ": " + value).join('\n')}${requestBody}`; - console.log(res); - // console.log(res.headers); - let response = 'NULL'; - console.log(`================================\nREPORT\n================================\n\nIP: ${ip}\n----------------\nACTION:\n----------------\n${action}\n----------------\nRESPONSE:\n----------------\n${response}`); + let response = !res.isError ? '' : `\n----------------\nError raised:\n----------------\n${JSON.stringify(res.responseData)}`; + //console.log(`================================\nREPORT\n================================\n\nIP: ${ip}\n----------------\nACTION:\n----------------\n${action}${response}`); + let loggerFolder = config().logger_folder; + loggerFolder = !['/', '\\'].includes(loggerFolder.at(-1)) ? loggerFolder + '/' : loggerFolder; + + fs.writeFileSync( + `${loggerFolder}${date.getTime()}.log`, + `================================\nREPORT\n================================\n\nIP: ${ip}\n----------------\nACTION:\n----------------\n${action}${response}` + ); } module.exports = async (req, res, next) => { diff --git a/logger/1696077968979.log b/logger/1696077968979.log new file mode 100644 index 0000000..0cea564 --- /dev/null +++ b/logger/1696077968979.log @@ -0,0 +1,32 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +if-none-match: W/"21-KvcCk05a0ejgEOcKiMGP3HJAaIw" +---------------- +Error raised: +---------------- +{"error":"METHOD_NOT_FOUNDED","details":{}} \ No newline at end of file diff --git a/logger/1696078110139.log b/logger/1696078110139.log new file mode 100644 index 0000000..6f064ec --- /dev/null +++ b/logger/1696078110139.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078148809.log b/logger/1696078148809.log new file mode 100644 index 0000000..6f064ec --- /dev/null +++ b/logger/1696078148809.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078152801.log b/logger/1696078152801.log new file mode 100644 index 0000000..6f064ec --- /dev/null +++ b/logger/1696078152801.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078155304.log b/logger/1696078155304.log new file mode 100644 index 0000000..2434d7b --- /dev/null +++ b/logger/1696078155304.log @@ -0,0 +1,32 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +pragma: no-cache +cache-control: no-cache +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078172286.log b/logger/1696078172286.log new file mode 100644 index 0000000..6f064ec --- /dev/null +++ b/logger/1696078172286.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078360031.log b/logger/1696078360031.log new file mode 100644 index 0000000..06bb568 --- /dev/null +++ b/logger/1696078360031.log @@ -0,0 +1,27 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 POST /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +user-agent: python-requests/2.27.1 +accept-encoding: gzip, deflate +accept: */* +connection: keep-alive +content-length: 8 +content-type: application/x-www-form-urlencoded +~~~~~~~~~~~~~~ +[REQUEST BODY] +~~~~~~~~~~~~~~ +asdasd=1 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"POST"}} \ No newline at end of file diff --git a/logger/1696078390481.log b/logger/1696078390481.log new file mode 100644 index 0000000..6f064ec --- /dev/null +++ b/logger/1696078390481.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078402689.log b/logger/1696078402689.log new file mode 100644 index 0000000..8da7837 --- /dev/null +++ b/logger/1696078402689.log @@ -0,0 +1,27 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 POST /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +user-agent: python-requests/2.27.1 +accept-encoding: gzip, deflate +accept: */* +connection: keep-alive +content-length: 8 +content-type: application/x-www-form-urlencoded +~~~~~~~~~~~~~~ +[REQUEST BODY] +~~~~~~~~~~~~~~ +asdasd=1 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"POST","content-type":"application/x-www-form-urlencoded"}} \ No newline at end of file diff --git a/logger/1696078452280.log b/logger/1696078452280.log new file mode 100644 index 0000000..6f064ec --- /dev/null +++ b/logger/1696078452280.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078455190.log b/logger/1696078455190.log new file mode 100644 index 0000000..2434d7b --- /dev/null +++ b/logger/1696078455190.log @@ -0,0 +1,32 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +pragma: no-cache +cache-control: no-cache +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET"}} \ No newline at end of file diff --git a/logger/1696078464012.log b/logger/1696078464012.log new file mode 100644 index 0000000..15d8dda --- /dev/null +++ b/logger/1696078464012.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET","content-type":null}} \ No newline at end of file diff --git a/logger/1696078471980.log b/logger/1696078471980.log new file mode 100644 index 0000000..8da7837 --- /dev/null +++ b/logger/1696078471980.log @@ -0,0 +1,27 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 POST /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +user-agent: python-requests/2.27.1 +accept-encoding: gzip, deflate +accept: */* +connection: keep-alive +content-length: 8 +content-type: application/x-www-form-urlencoded +~~~~~~~~~~~~~~ +[REQUEST BODY] +~~~~~~~~~~~~~~ +asdasd=1 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"POST","content-type":"application/x-www-form-urlencoded"}} \ No newline at end of file diff --git a/logger/1696078550536.log b/logger/1696078550536.log new file mode 100644 index 0000000..15d8dda --- /dev/null +++ b/logger/1696078550536.log @@ -0,0 +1,31 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 GET /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +connection: keep-alive +cache-control: max-age=0 +sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102" +sec-ch-ua-mobile: ?0 +sec-ch-ua-platform: "Windows" +upgrade-insecure-requests: 1 +user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0 (Edition Yx GX) +accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 +sec-fetch-site: none +sec-fetch-mode: navigate +sec-fetch-user: ?1 +sec-fetch-dest: document +accept-encoding: gzip, deflate, br +accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"GET","content-type":null}} \ No newline at end of file diff --git a/logger/1696078647763.log b/logger/1696078647763.log new file mode 100644 index 0000000..8da7837 --- /dev/null +++ b/logger/1696078647763.log @@ -0,0 +1,27 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 POST /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +user-agent: python-requests/2.27.1 +accept-encoding: gzip, deflate +accept: */* +connection: keep-alive +content-length: 8 +content-type: application/x-www-form-urlencoded +~~~~~~~~~~~~~~ +[REQUEST BODY] +~~~~~~~~~~~~~~ +asdasd=1 +---------------- +Error raised: +---------------- +{"error":"METHOD_MUST_BE_POST_JSON","details":{"request_method":"POST","content-type":"application/x-www-form-urlencoded"}} \ No newline at end of file diff --git a/logger/1696079964979.log b/logger/1696079964979.log new file mode 100644 index 0000000..fa69df6 --- /dev/null +++ b/logger/1696079964979.log @@ -0,0 +1,23 @@ +================================ +REPORT +================================ + +IP: 127.0.0.1 +---------------- +ACTION: +---------------- +HTTP 1.1 POST /?response_format=json +~~~~~~~~~ +[HEADERS] +~~~~~~~~~ +host: localhost:8080 +user-agent: python-requests/2.27.1 +accept-encoding: gzip, deflate +accept: */* +connection: keep-alive +content-length: 13 +content-type: application/json +~~~~~~~~~~~~~~ +[REQUEST BODY] +~~~~~~~~~~~~~~ +{"asdasd": 1} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5805d01..8da94f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,8 @@ "": { "dependencies": { "body": "^5.1.0", + "body-parser": "^1.20.2", + "cookie-parser": "^1.4.6", "express": "^4.18.2", "sequelize": "^6.33.0" } @@ -62,12 +64,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -75,7 +77,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -153,6 +155,26 @@ "node": ">= 0.6" } }, + "node_modules/cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "dependencies": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/cookie-parser/node_modules/cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", @@ -263,6 +285,43 @@ "node": ">= 0.10.0" } }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -566,9 +625,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", @@ -940,12 +999,12 @@ } }, "body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "requires": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -953,7 +1012,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" } @@ -995,6 +1054,22 @@ "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==" }, + "cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "requires": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "dependencies": { + "cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==" + } + } + }, "cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", @@ -1087,6 +1162,38 @@ "type-is": "~1.6.18", "utils-merge": "1.0.1", "vary": "~1.1.2" + }, + "dependencies": { + "body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "requires": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + } + }, + "raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "requires": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + } } }, "finalhandler": { @@ -1302,9 +1409,9 @@ "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" }, "raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "requires": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index 8249bd7..5818b22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,8 @@ { "dependencies": { "body": "^5.1.0", + "body-parser": "^1.20.2", + "cookie-parser": "^1.4.6", "express": "^4.18.2", "sequelize": "^6.33.0" } diff --git a/server.js b/server.js index eba5b41..9fc3339 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,31 @@ const express = require('express'); const bodyHand = require('body'); const config = require('./config-handler'); +const http = require('http'); const app = express(); +http.ServerResponse.prototype.errorModeOn = function () { + this.isError = true; +} + +http.ServerResponse.prototype.bindNext = function (next) { + this.next = next; +} + +http.ServerResponse.prototype.sendModed = function (sendData) { // Модифицируем res.send + if (sendData !== undefined && config().logger_mode) { + this.responseData = sendData; + require('./logger')(this.req, this, this.next); + } + this.send(sendData); +} + +app.use((req, res, next) => { + res.bindNext(next); + next(); +}); + app.use(async (req, res, next) => { // Для добавления оригинального тела запроса const body = bodyHand(req, res, { limit: 9999999999, @@ -12,9 +34,17 @@ app.use(async (req, res, next) => { // Для добавления оригин }, (err, body) => { if (!err) { req.byteBody = Buffer.from(body, 'base64'); - } - else { - // console.log(err); + + // Запись в req.json при json + if ( + !!req.headers && + req.headers['content-type']?.includes('json') + ) { + try { + req.json = JSON.parse(req.byteBody.toString('utf8')); + } + catch (_e) {} + } } next(); }); @@ -22,9 +52,9 @@ app.use(async (req, res, next) => { // Для добавления оригин app.use("/api", async (rq, rs, next) => next(), require('./api')); -app.get("/admin", async (req, res) => res.send('ok')); +// app.get("/admin", async (req, res) => res.send('ok')); -if (config().logger_mode) app.use(require('./logger'), require('./api')); // Логгирование +// app.use(require('./logger'), require('./api')); // Логгирование // Подключение через HTTPS let server;