如何从 feathers.js 服务重定向

How to redirect from a feathers.js service

我有一个 feathers.js 服务,我需要在使用 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 ?

我不确定这在羽毛方面有多少好的做法,但您可以在羽毛 params 上粘贴对 res 对象的引用,然后将其作为你请。

// declare this before your services
app.use((req, res, next) => {
    // anything you put on 'req.feathers' will later be on 'params'
    req.feathers.res = res;

    next();
});

然后在你的 class:

class Payment {
    // ..
    create(data, params) {
    // do some logic
    // redirect to an other page with 301|302
    params.res.redirect('http://some-page.com');

    // You must return a promise from service methods (or make this function async)
    return Promise.resolve();
    }
}

找到一种更友好的方式来做到这一点:

假设我们有定制服务:

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');
}

背后的想法是feathers.js使用express中间件,逻辑如下。

如果链接的中间件是一个 Object,那么在您可以链接任意数量的中间件之后,它会被解析为一个服务。

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