Javascript Loopback/Express:如何重定向到remoteMethod内的URL?

Javascript Loopback/Express:如何重定向到remoteMethod内的URL?,javascript,express,loopbackjs,loopback,Javascript,Express,Loopbackjs,Loopback,我很难找到任何关于在模型函数或remoteMethod中重定向到URL的文档。这里有人做过这个吗?请在下面找到我的代码 模型内的函数(公开/捕获端点) 我在这里找到了一个。您需要创建并访问resExpress对象。从那里,您可以使用res.redirect('someurl') 您可以从HTTP上下文获取响应对象,然后将其作为参数注入远程方法,并直接使用它: Model.remoteMethodName = function (data, res, next) { res.redirec

我很难找到任何关于在模型函数或
remoteMethod
中重定向到URL的文档。这里有人做过这个吗?请在下面找到我的代码

模型内的函数(公开/捕获端点) 我在这里找到了一个。您需要创建并访问
res
Express对象。从那里,您可以使用
res.redirect('someurl')


您可以从HTTP上下文获取响应对象,然后将其作为参数注入远程方法,并直接使用它:

Model.remoteMethodName = function (data, res, next) {
    res.redirect('https://host.name.com/path?data=${data}')
};

Model.remoteMethod('remoteMethodName', {
    http: {
        path: '/route',
        verb: 'get',
    },
    accepts: [
        {arg: 'data', type: 'string', required: false, http: {source: 'query'}},
        {arg: 'res', type: 'object', http: ctx => { return ctx.res; }},
    ],
    returns: [
        {arg: 'result', type: 'any'}
    ],
});

感谢@Iwan Bhakti S,这种方法更好,因为它避免了express.js抱怨http头,这些头可能在我们实际到达.afterRemote代码之前就已经发送了
Form.afterRemote('catch', (context, remoteMethodOutput, next) => {
  let res = context.res;
  res.redirect('http://google.be');
});
Model.remoteMethodName = function (data, res, next) {
    res.redirect('https://host.name.com/path?data=${data}')
};

Model.remoteMethod('remoteMethodName', {
    http: {
        path: '/route',
        verb: 'get',
    },
    accepts: [
        {arg: 'data', type: 'string', required: false, http: {source: 'query'}},
        {arg: 'res', type: 'object', http: ctx => { return ctx.res; }},
    ],
    returns: [
        {arg: 'result', type: 'any'}
    ],
});