midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.34k stars 573 forks source link

[Feature Request] 获取当前应用注册的所有路由 #165

Closed atzcl closed 3 years ago

atzcl commented 5 years ago

因为当前路由注册是分散式的,如果当某一路由没有命中时,想要排查起来会很麻烦,希望能提供获取当前应用注册的所有路由的方式。

建议: 1:dev 启动时,把所有的路由打印到控制台; 2: 启动时,写入 log; 3: 提供 cli 命令查看;

czy88840616 commented 5 years ago

好,马上做。 atzcl notifications@github.com于2019年3月9日 周六下午3:15写道:

因为当前路由注册是分散式的,如果当某一路由没有命中时,想要排查起来会很麻烦,希望能提供获取当前应用注册的所有路由的方式。

建议: 1:dev 启动时,把所有的路由打印到控制台; 2: 启动时,写入 log; 3: 提供 cli 命令查看;

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/midwayjs/midway/issues/165, or mute the thread https://github.com/notifications/unsubscribe-auth/AAZkBMdLOjgYdTU9wyKap5HvQF-kEk4Yks5vU1-YgaJpZM4bmh90 .

ZQun commented 5 years ago

这里有个打印请求信息的中间件,可以查看请求路由信息等

console log: 2019-03-10 10:53:51,666 INFO 15451 [-/255.230.133.135/-/662ms GET /Wechat.API/GetProAuthCode] [access] 255.230.133.135: - GET /Wechat.API/GetProAuthCode HTTP/404 - - 657 - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36

// src/app/middleware/access.ts

import * as path from 'path';
import * as util from 'util';

export = () => {
    const skipExt = ['.png', '.jpeg', '.jpg', '.ico', '.gif'];
    return async (ctx: any, next: any) => {
        const start = new Date().getTime();
        await next();

        const rs = Math.ceil(new Date().getTime() - start);

        ctx.set('X-Response-Time', rs.toString());

        const ext = path.extname(ctx.url).toLocaleLowerCase();
        const isSkip = skipExt.indexOf(ext) !== -1 && ctx.status < 400;

        if (!isSkip) {
            const ip = ctx.get('X-Real-IP') || ctx.ip;
            const port = ctx.get('X-Real-Port');
            const protocol = ctx.protocol.toUpperCase();
            const method = ctx.method;
            const url = ctx.url;
            const status = ctx.status;
            const length = ctx.length || '-';
            const referrer = ctx.get('referrer') || '-';
            const ua = ctx.get('user-agent') || '-';
            const serverTime = ctx.response.get('X-Server-Response-Time') || '-';
            const message = util.format('[access] %s:%s - %s %s %s/%s %s %s %s %s %s',
                ip, port, method, url, protocol, status, length, referrer, rs, serverTime, ua);
            ctx.logger.info(message);
        }
    };
};

// src/config/config.local.ts

config.middleware = ['access'];
czy88840616 commented 5 years ago

访问的请求其实egg已经做了,现在每次请求本身就会有打印出来的,我想缺失的应该是路由匹配的部分,比如写了某个路由,但是没有生效的时候,需要排查一下。

atzcl commented 5 years ago

@czy88840616 对的,就跟 laravel 的 php artisan route:list 命令一样,可以查看当前应用注册的所有路由, image

zhou-z-xin commented 5 years ago

这个Api写出来了吗?

czy88840616 commented 5 years ago

还木有。。

enochjs commented 4 years ago

我想在代码中获取所有注册过的路由,有什么办法么?

enochjs commented 4 years ago

访问的请求其实egg已经做了,现在每次请求本身就会有打印出来的,我想缺失的应该是路由匹配的部分,比如写了某个路由,但是没有生效的时候,需要排查一下。

我想在代码中获取所有注册过的路由,有什么办法么?

czy88840616 commented 4 years ago

@enochjs 上周做了生成 router.json 的工作,已经可以 dump 出路由,如果需要在代码中拿的话,可以参考 https://github.com/midwayjs/midway/blob/master/packages/midway-web/src/midway.ts#L96

tkvern commented 3 years ago

这个功能出来了么,催更一下 @czy88840616

czy88840616 commented 3 years ago

有了,已经有路由表了。https://www.yuque.com/midwayjs/midway_v2/router_table