move http request to method handleHTTPRequest

This commit is contained in:
srgooglo 2022-05-06 08:07:34 +02:00
parent d0f9b7c1d6
commit 9b21671be8

View File

@ -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)