简体   繁体   English

Arrow API中的多路路由

[英]Multipe Routes in Arrow API

Is there a way to declare multiple API routes from a single file in Arrow? 有没有一种方法可以从Arrow中的单个文件声明多个API路由?

Example: Say you want to declare multiple endpoints for a user API: 示例:假设您要为用户API声明多个端点:

  • GET /api/user/:id GET / api / user /:id
  • DELETE /api/user/:id/delete 删除/ api / user /:id / delete
  • POST /api/user POST / api /用户

It would make sense to keep these in the same file since they are related and could share code, instead of splitting them into their own files. 将它们保留在同一个文件中是有意义的,因为它们是相关的并且可以共享代码,而不是将它们拆分为自己的文件。

I'm referring to these docs . 我指的是这些文档

At this moment the only way to keep them in the same file is to use ALL as the method and then in the action use req.method to delegate to the right logic. 目前,将它们保留在同一文件中的唯一方法是使用ALL作为方法,然后在操作中使用req.method委托给正确的逻辑。 Eg: 例如:

..
  method: 'ALL',
  action: function(req, res, next) {
    switch (req.method) {
      case 'GET':
        ..
        break;
      case 'DELETE':
        ..
        break;
      default:
        return res.notfound(next);
        break;
    }
  }
..

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM