From 9b21671be8c4c87d93eba5a437b72abb3f05c232 Mon Sep 17 00:00:00 2001 From: srgooglo Date: Fri, 6 May 2022 08:07:34 +0200 Subject: [PATCH] move http request to method `handleHTTPRequest` --- src/server/index.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/server/index.js b/src/server/index.js index 80835db..990e437 100755 --- a/src/server/index.js +++ b/src/server/index.js @@ -192,24 +192,7 @@ class Server { }, } - this.httpInterface[endpoint.method](endpoint.route, ...middlewares, async (req, res) => { - try { - // check if endpoint is disabled - if (!this.endpointsMap[endpoint.method][endpoint.route].enabled) { - throw new Error("Endpoint is disabled!") - } - - return await endpoint.fn(req, res) - } catch (error) { - if (typeof this.params.onRouteError === "function") { - return this.params.onRouteError(req, res, error) - } else { - return res.status(500).json({ - "error": error.message - }) - } - } - }) + this.httpInterface[endpoint.method](endpoint.route, ...middlewares, this.handleHTTPRequest) } registerWSEndpoint = (endpoint, ...execs) => { @@ -273,6 +256,25 @@ class Server { } // handlers + handleHTTPRequest = async (req, res) => { + try { + // check if endpoint is disabled + if (!this.endpointsMap[endpoint.method][endpoint.route].enabled) { + throw new Error("Endpoint is disabled!") + } + + return await endpoint.fn(req, res) + } catch (error) { + if (typeof this.params.onRouteError === "function") { + return this.params.onRouteError(req, res, error) + } else { + return res.status(500).json({ + "error": error.message + }) + } + } + } + handleWSClientConnection = async (socket) => { socket.res = (...args) => { socket.emit("response", ...args)