我有一个羽毛.js服务,我需要在使用帖子时重定向到特定页面
class Payment {
// ..
create(data, params) {
// do some logic
// redirect to an other page with 301|302
return res.redirect('http://some-page.com');
}
}
是否有可能从羽毛.js服务重定向?
我不确定这在羽毛中会有多少好的做法,但你可以在羽毛的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 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
使用快速中间件,逻辑如下。
如果链接的中间件是Object
,则在您可以链接任意数量的中间件之后,它被解析为服务。
app.use('api/v1/messages', middleware1, feathersService, middleware2)