Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何从feathers.js服务重定向_Node.js_Feathersjs - Fatal编程技术网

Node.js 如何从feathers.js服务重定向

Node.js 如何从feathers.js服务重定向,node.js,feathersjs,Node.js,Feathersjs,我有一个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'); } } 是否可以从feathers.js服务重定向 我不确定这在fea

我有一个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');
   }
}
是否可以从feathers.js服务重定向


我不确定这在feathers中有多好,但您可以在feathers的
参数
上粘贴一个
res
对象的引用,然后按照您的意愿处理它

//在提供服务之前声明此项
应用程序使用((请求、恢复、下一步)=>{
//您在“req.feathers”上添加的任何内容稍后都将显示在“params”上
req.feathers.res=res;
next();
});
然后在你们班上:

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中间件,逻辑如下

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

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