I'm a beginner to Nodejs, and I have a side project to check on the status of my endpoints.
I initially tried to do it with XMLHttp, but I got my requests blocked because of CORS. Now I want to try to consume those API points with node and if it returns a 200 paint the row green if its down paint the row red.
I have the following code for my server.js
var express = require('express');
var app = express();
var request = require('request');
var port = process.env.PORT || 3000;
app.set('port', (port));
app.use(express.static(__dirname + '/'));
app.get('/', function(request, response) {
response.render('/');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
var services = [
{
url: 'MyGetEndpoint'
},
{
url: 'MyGetEndpoint'
}
];
function checkService( url ) {
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response)
}
else{
console.log(error);
}
});
}
function loadDoc() {
var url;
for(var i in services){
url = services[i].url;
checkService( url );
}
}
loadDoc();
On the terminal, I can see that my two endpoints are sent back, so in my frontend, if an endpoint is the status 200, I want to color the background.
<div id="result" class="media text-muted pt-3">
How can I fetch the URLs from my server? Do I need to do another javascript using ajax to call my loadDoc or can I send directly to the browser?
Thanks in advance, any help is highly appreciated. I read the following question which is similar to mine, but I couldn't fully understand what they meant. Here