node.jsfeathersjs

How to redirect from a feathers.js service


I have a feathers.js service and I need to redirect to a specific page when using post

class Payment {
   // ..
   create(data, params) {
      // do some logic
      // redirect to an other page with 301|302

      return res.redirect('http://some-page.com');
   }
}

Is there a posibility to redirect from a feathers.js service ?


Solution

  • Found a way to do this in a more friendly manner:

    Assuming we have a custom service:

    app.use('api/v1/messages', {
      async create(data, params) {
        // do your logic
    
        return // promise
      }
    }, redirect);
    
    function redirect(req, res, next) {
      return res.redirect(301, 'http://some-page.com');
    }
    

    The idea behind is that feathers.js use express middlewares and the logic is the following one.

    If the chained middleware is an Object, then it is parsed as a service, after you can chain any number of middlewares you want.

    app.use('api/v1/messages', middleware1, feathersService, middleware2)