简体   繁体   English

如何获得Koa服务器URL路由列表

[英]How can i get a list of Koa server url routes

I'm developing a mock server using koajs, and i would like to publish a service which lists developed APIs. 我正在使用koajs开发一个模拟服务器,我想发布一个列出开发API的服务。

I use koa-router for mouting services. 我使用koa-router进行mouting服务。

And i would like somethink like: 我想要一些想法:

var business_router = require('./controllers/router');
app.use(business_router.routes());   
app.use(business_router.allowedMethods());

console.log(app.listRoutes());

Although I guess it is not part of the official koa-router API, you can do as following: 虽然我猜它不是官方koa-router API的一部分,但你可以这样做:

var app = require('koa')();
var router = require('koa-router')();

router.get('/bar', function*() { this.body = 'Hi'; }});
router.get('/bar/foo', function*() { this.body = 'Hi'; }});
router.get('/foo', function*() { this.body = 'Hi'; }});
router.get('/bar/baz', function*() { this.body = 'Hi'; }});

app
  .use(router.routes())
  .use(router.allowedMethods());

console.log(router.stack.map(i => i.path));
// ['/bar', '/bar/foo', '/foo', '/bar/baz']

In your case, assuming business_router is an instance of koa-router : 在您的情况下,假设business_routerkoa-router一个实例:

console.log(business_router.stack.map(i => i.path));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的 koa 路由拆分为单独的文件? - How can I split my koa routes into separate files? 如何将我的 koa 路由拆分为单独的文件? 中间件问题 - How can I split my koa routes into separate files? Middleware problem 如何使用ES6将这个路由文件(koa @ next — v2)转换为具有继承性的类? - How can I turn this routes file (koa@next — v2) into a class with inheritance using ES6? 如何配置 koa-router 在任何一组路由之前运行公共代码? - How can I configure koa-router to run common code before any of a certain group of routes? 如何获取我在Restify服务器中使用的所有路由的列表 - How to get list of all routes I am using in restify server Koa server--如何在中间件之间传递服务器中的数据,以便我可以将它传递给前端 - Koa server-- How to pass data in the server between middlewares so I can pass it to the front end 如何在 koa 中使用“http”模块? - How can I use the "http" module in koa? 如何在 Koa 中做一个条件路由器? - How can I do a conditional router in Koa? 如何在KOA中使用regexp匹配ctx.url,例如“ /?id = xxx”和“ /”? - How can I use regexp to match ctx.url like ''/?id=xxx'' and “/” in koa? 使用单独的客户端和服务器应用程序,如何在带有Koa的Node中获取用户的IP地址? - With separate client and server apps, how do I get a user's IP address, in Node with Koa?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM