From 9db7a4127f597635b5ca6f5e94ef61f858f7df95 Mon Sep 17 00:00:00 2001 From: Nikiroy78 <35032449+Nikiroy78@users.noreply.github.com> Date: Fri, 6 Oct 2023 05:05:19 +0300 Subject: [PATCH] Fix bug with upload files --- api/v1/index.js | 7 ++-- config.json | 4 +- frontend/public/js/api-module.js | 1 + server.js | 69 +++++++------------------------- 4 files changed, 23 insertions(+), 58 deletions(-) diff --git a/api/v1/index.js b/api/v1/index.js index bb59336..528e7ce 100644 --- a/api/v1/index.js +++ b/api/v1/index.js @@ -1,4 +1,5 @@ -const router = require("express").Router(); +const express = require("express"); +const router = express.Router(); const fs = require("fs"); const config = require('../../config-handler'); const database = require('../../database'); @@ -8,8 +9,8 @@ const bodyParser = require('body-parser'); //router.use(require('cookie-parser')()); // Парсим JSON -router.use(bodyParser.json({ type: 'application/*+json' })) -router.use(bodyParser.json({ type: 'application/json' })) +router.use(bodyParser.json({ type: 'application/*+json' })); +router.use(bodyParser.json({ type: 'application/json' })); const unknownError = (err) => { const stackId = new Date().getTime(); diff --git a/config.json b/config.json index 4ea7ddd..9b1e823 100644 --- a/config.json +++ b/config.json @@ -20,5 +20,7 @@ "authors_blacklist" : [ "Монеточка", "Monetochka" - ] + ], + + "request_size_limit" : "150mb" } \ No newline at end of file diff --git a/frontend/public/js/api-module.js b/frontend/public/js/api-module.js index ac05a05..1033b55 100644 --- a/frontend/public/js/api-module.js +++ b/frontend/public/js/api-module.js @@ -84,6 +84,7 @@ class Api { xhr.onreadystatechange = function (event) { //console.log(event); if (this.readyState != 4) return; + console.log(this.responseText); cb(JSON.parse(this.responseText).response); }; xhr.send( diff --git a/server.js b/server.js index d02e48f..f507802 100644 --- a/server.js +++ b/server.js @@ -6,64 +6,25 @@ const config = require("./config-handler"); // const http = require("http"); const { Database } = require("./database"); const { Logger } = require('./logger'); +const bodyParser = require('body-parser'); const app = express(); +// Выставляем адекватное ограничение на тело запроса +app.use(bodyParser.json({limit: config().request_size_limit})); -// global.database = new Database( - // config().database.address, - // config().database.port, - // config().database.username, - // config().database.password, - // config().database.database -// ); - -/*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); +app.use( + (req, res, next) => { + // Поставил по-умолчанию + res.result = function (data, isErr = false, code = 200) { + this.status(code).json(!isErr ? { + response : data + } : { + error : data + }); + }; + next(); } - this.send(sendData); -}; - -app.use((req, res, next) => { - res.bindNext(next); - next(); -}); - -app.use((req, res, next) => { - // Для добавления оригинального тела запроса - const body = bodyHand( - req, - res, - { - limit: 9999999999, - cache: false, - encoding: "base64", - }, - (err, body) => { - if (!err) { - req.byteBody = Buffer.from(body, "base64"); - - // Запись в req.json при json - if (!!req.headers && req.headers["content-type"]?.includes("json")) { - try { - req.json = JSON.parse(req.byteBody.toString("utf8")); - } catch (_e) {} - } - } - next(); - }, - ); -});*/ +); app.use(async (req, res, next) => { req.logger = new Logger();