support method map

This commit is contained in:
srgooglo 2021-06-21 13:22:21 +02:00
parent 6c1d3cb576
commit 11a6b1d260

View File

@ -94,6 +94,8 @@ class Server {
const endpoint = { method: method, route: route, controller: controller } const endpoint = { method: method, route: route, controller: controller }
this.endpoints[route] = endpoint
this.routes.push(route) this.routes.push(route)
this.httpServer[method.toLowerCase()](route, (req, res, next) => this.handleRequest(req, res, next, endpoint)) 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!`) throw new Error(`Invalid endpoint!`)
} }
// add to endpoints map
this.endpoints[endpoint.route] = endpoint
try { try {
let { method, route, controller, fn } = endpoint let { method, route, controller, fn } = endpoint
@ -221,8 +220,19 @@ class Server {
}) })
this.registerEndpoint("get", "/map", (req, res) => { 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({ res.json({
routes: this.routes routes: this.routes,
methods: methods
}) })
}) })