Encountering an issue with page refreshes, I'm struggling to make it work smoothly. The process begins with filling out a form on an HTML page, which then posts to an API on a different service using the Node RequestJS module. After the post, the other service performs certain tasks on its end, and I then make a get request to retrieve the ID of the completed job.
With the obtained ID, I trigger another get request to fetch the logs for the completed job. However, since the logs are still being populated, I need a function to refresh the webpage (which is a simple ExpressJS served page, not actual HTML) until the logs are complete.
The challenge lies in getting the refresh function to actually work.
The code in my main.js file (linked to the HTML page):
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)
}
And the code in my ExpressJS (server-side code):
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)
})
})
Attempts to use location.reload and location = location.href in main.js have been unsuccessful. It seems the refresh is happening too quickly, so I attempted to slow it down with a loop.
In the provided code, I suspect my timed refresh loop is failing due to the location.href just above it.
Any advice or guidance on this issue would be greatly appreciated.