我好像有什么问题,让一个页面刷新。我在HTML页面上填写了一个表单,然后通过Node RequestJS模块将表单发布到不同服务上的API。在发送帖子之后,其他服务会自己执行一些任务,我使用get请求来检索完成的工作的ID。
一旦我有了这个ID,我就会使用它来启动另一个get请求,获得完成工作的日志。当然,日志在这一点上仍然是填充的,所以我想要一个函数来刷新网页,这是一个简单的高速公路服务页面,而不是真正的html,直到它得到日志已经完成的消息。
但是,我现在的主要问题是如何使刷新本身实际工作。
我的main.js中的代码(连接到HTML页面)
request({
url: 'http://'+host+'/hwChange',
method: "POST",
headers: {
"content-type": "application/json",
},
body: jSON
})
setTimeout(function(){
request({
url: 'http://'+host+'/lastWF',
method: "GET",
})
}, 1000)
setTimeout(function(){location.href='/log'}, 2000)
timer = 2000
for ( i=0;i<30;i++){
timer = timer+2000
setTimeout(function(){
request({
url: 'http://'+host+'/log',
method: "GET"
})
},timer)
}和代码在我的高速公路(服务器端)
app.post('/hwChange', jsonParser, function (req,res) {
if (!req.body) return res.sendStatus(500)
request({
url: 'https://'+user+':'+pass+'@orcprod.adeo.no:443/vco/api/workflows/9edb9c28-2e71-4d41-a789-f7d94ee4128a/executions',
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(req.body)
}, function (error, response, body){
console.log(response)
})
})
app.get('/lastWF', function (req,res){
request.get('https://'+user+':'+pass+'@orcprod.adeo.no:443/vco/api/workflows/9edb9c28-2e71-4d41-a789-f7d94ee4128a/executions', function (error, response, body){
if (!error && response.statusCode == 200) {
//console.log(body)
}
var newest = JSON.parse(body)
newest = newest.relations.link
var arr = []
for (var i = newest.length - 1; i >= 2; i--) {
var now = newest[i].attributes[1].value
arr.push(new Date(now))
}
var maxDate = new Date(Math.max.apply(null, arr))
for (var i = newest.length - 1; i >= 2; i--) {
var now = newest[i].attributes[1].value
if ( new Date(now).getTime() == maxDate.getTime() ) {
WFid = newest[i].attributes[0].value
}
}
res.end("<BR><BR>All done!")
})
})
app.get('/log', function (req, res) {
request.get('https://'+user+':'+pass+'@orcprod.adeo.no:443/vco/api/workflows/9edb9c28-2e71-4d41-a789-f7d94ee4128a/executions/'+WFid+'/logs', function (error, response, body){
if (!error && response.statusCode == 200) {
//console.log(body)
}
var logs = JSON.parse(body)
for ( i = logs.logs.length -1 ; i>=0; i-- ){
res.write(logs.logs[i].entry["short-description"]+"<BR>")
}
res.end("Done "+WFid)
})
})我尝试过在main.js中使用main.js,以及location = location.href,但它似乎不起作用。我认为这是因为刷新发生得太快,所以我尝试用循环来减缓它的速度。
在我在这里发布的代码中,我认为我的时间刷新循环失败是因为location.href就在上面。
如有任何指示,将不胜感激。
发布于 2017-09-14 12:20:14
最后使用res.render和JADE,以便在日志的最后一条消息未收到时将元标记插入到网页中。如果有,就移除这个元标签。这取得了预期的效果。一旦加载/log,它现在每2秒更新一次页面,直到最后一条日志消息出现并停止更新。
https://stackoverflow.com/questions/46172921
复制相似问题