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

Jor*_*nve 3 javascript loopback express loopbackjs

我很难找到任何有关重定向到模型函数或remoteMethod. 这里有人已经这样做了吗?请在下面找到我的代码。

模型内的函数(暴露 /catch 端点)

Form.catch = function (id, data, cb) {
    Form.findById(id, function (err, form) {

      if (form) {
        form.formentries.create({"input": data},
          function(err, result) {
            /*
             Below i want the callback to redirect to a url  
             */
            cb(null, "http://google.be");
          });
      } else {
        /*
         console.log(err);
         */
        let error = new Error();
        error.message = 'Form not found';
        error.statusCode = 404;
        cb(error);
      }
    });
    };

    Form.remoteMethod('catch', {
    http: {path: '/catch/:id', verb: 'post'},
    description: "Public endpoint to create form entries",
    accepts: [
      {arg: 'id', type: 'string', http: {source: 'path'}},
      {arg: 'formData', type: 'object', http: {source: 'body'}},
    ],
    returns: {arg: 'Result', type: 'object'}
    });
Run Code Online (Sandbox Code Playgroud)

小智 7

我在这里找到了答案。您需要创建一个远程挂钩并访问resExpress 对象。从那里,您可以使用res.redirect('some url').

Form.afterRemote('catch', (context, remoteMethodOutput, next) => {
  let res = context.res;
  res.redirect('http://google.be');
});
Run Code Online (Sandbox Code Playgroud)