[英]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');
}
}
是否可以從feathers.js服務重定向?
找到了一種更友好的方式:
假設我們有自定義服務:
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)
我不確定羽毛中有多少良好練習,但是你可以在羽毛的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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.