From 11a6b1d26058916350a71d7eeda5daeb9e4e1e7d Mon Sep 17 00:00:00 2001 From: srgooglo Date: Mon, 21 Jun 2021 13:22:21 +0200 Subject: [PATCH] support method map --- src/server/index.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/server/index.js b/src/server/index.js index 02d4c2f..cf62718 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -91,8 +91,10 @@ class Server { if (typeof controller === "function") { controller = new classes.Controller(route, controller) } - + const endpoint = { method: method, route: route, controller: controller } + + this.endpoints[route] = endpoint this.routes.push(route) this.httpServer[method.toLowerCase()](route, (req, res, next) => this.handleRequest(req, res, next, endpoint)) @@ -167,9 +169,6 @@ class Server { throw new Error(`Invalid endpoint!`) } - // add to endpoints map - this.endpoints[endpoint.route] = endpoint - try { let { method, route, controller, fn } = endpoint @@ -221,8 +220,19 @@ class Server { }) this.registerEndpoint("get", "/map", (req, res) => { + const methods = {} + + this.routes.forEach((route) => { + const endpoint = this.endpoints[route] ?? {} + + if (typeof endpoint.method === "string") { + methods[route] = endpoint.method + } + }) + res.json({ - routes: this.routes + routes: this.routes, + methods: methods }) })