Multipe Routes in Arrow API

101 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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. E.g.:

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