简体   繁体   中英

Multipe Routes in Arrow API

Is there a way to declare multiple API routes from a single file in Arrow?

Example: Say you want to declare multiple endpoints for a user API:

  • GET /api/user/:id
  • DELETE /api/user/:id/delete
  • POST /api/user

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. Eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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