Node.js 如何从feathers.js服务重定向
我有一个feathers.js服务,在使用post时需要重定向到特定页面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
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)