【问题标题】:Node Express Route dynamic methodNode Express Route动态方法
【发布时间】:2022-01-20 22:08:20
【问题描述】:

我希望方法是动态的

const method = METHODS?.[config.method] ?? 'get';
const endpoint = config.endpoint;
const router = express.Router();

这行得通

app['post'](endpoint, execute);

这不行

app[method](endpoint, execute);

【问题讨论】:

    标签: node.js express routes router


    【解决方案1】:

    我声明了错误的类型。

    改成

    export interface RouteConfig {
      endpoint: METHODS;
      method: string;
      authorizer?: string;
      handler: string;
    }
    

    export interface RouteConfig {
      endpoint: string;
      method: METHODS;
      authorizer?: string;
      handler: string;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2019-05-12
      • 2022-01-04
      • 1970-01-01
      • 2015-07-17
      相关资源
      最近更新 更多