Loopback / Express:如何重定向到 remoteMethod 内的 URL?
Loopback / Express: How to redirect to URL inside a remoteMethod?
我很难找到任何关于重定向到模型函数内的 URL 或 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'}
});
我找到了一个 answer here. You need to create a remote hook 并访问了 res
Express 对象。从那里,您可以使用 res.redirect('some url')
.
Form.afterRemote('catch', (context, remoteMethodOutput, next) => {
let res = context.res;
res.redirect('http://google.be');
});
您可以从 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'}
],
});
我很难找到任何关于重定向到模型函数内的 URL 或 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'}
});
我找到了一个 answer here. You need to create a remote hook 并访问了 res
Express 对象。从那里,您可以使用 res.redirect('some url')
.
Form.afterRemote('catch', (context, remoteMethodOutput, next) => {
let res = context.res;
res.redirect('http://google.be');
});
您可以从 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'}
],
});