现在,我有一个通过表单发布的 Express 路线,下面是一个 chop 的示例。初始表单位于 iframe 内,因此在收到来自 http://example.com/endpoint
的响应后,我将响应发送回 iframe,其中包含指向“签名”页面的链接,定位父框架。
不幸的是,来自 http://example.com/endpoint
的响应可能需要很长时间,这会导致 iframe 超时并且永远不会收到响应。我想做的是立即将某种类型的响应发送回 iframe 并将页面重定向到某种“加载页面”——这将在路由器等待来自 http://的响应时显示example.com/endpoint
.
我现在正在使用 Express 向用户提供包含 iframe 的页面 - 所有 View 都在服务器端控制。
我想知道是否有人可以向我提供任何资源,或者可以将我推向正确的方向。
router.post('/api/orders', function(req, res) {
var order = {
'model': req.body.model,
'options': optionsArray
}
request.post({
url: 'http://example.com/endpoint,
body: order,
json: true
}, function(err, response, body) {
if (!error && response.statusCode === 200) {
if (!body.isCustom) {
hellosign.embedded.getSignUrl(body.signatureId)
.then(function(response) {
var signatureUrl = response.embedded.sign_url;
var resSignatureUrl = encodeURIComponent(signatureUrl);
res.send('<a href="http://' + req.headers.host + '/order/sign/' + body.orderNumber + '?url=' + resSignatureUrl + '" target="_parent">Click to sign</a>');
})
.catch(function(err) {
console.log(err);
})
} else {
res.send('You selected custom options.');
}
}
if (error || response.statusCode === 403) {
res.json({
message: 'something went wrong with your order',
errorCode: response.statusCode,
errorMessage: body.message
});
}
});
});
最佳答案
我会将 hellosign/任何长时间运行的 API 调用放入其自己的模块中。单独测试以确保其正常工作。
然后您的 iframe 或其他内容(您真的需要 iframe 吗?)发送请求,该请求只是一个“开始订单”请求,该请求从 hellosign 模块获取订单 id。然后使用 setInterval 或其他方法来检查新端点,即“orderstatus”端点。
您的 orderstatus 端点访问新的 hellosign 模块以检查订单的状态。
所以它可能是这样的:
post('/start', function(req,res) {
var id = hellosign.startorder(req.body.model);
res.send(id);
});
get('/status', function(req,res) {
res.send(hellosign.checkstatus(req.body.id));
}
// hellosign.js
var status = {};
exports.startorder = function(model) {
var id = uuid.v4(); // some unique id;
status[id] = 'started';
request.post(api, /* ... */ ).then(function(signurl) { status[id] = signurl });
return id;
}
exports.checkstatus = function(id) {
return status[id];
}
关于javascript - 使用 Express 执行功能时重定向客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39736360/