I am new to Nodejs and I have a side project to check the status of my endpoints.
At first I tried to do this using XMLHttp, but my requests were blocked due to CORS. Now I want to try to use these API points using node, and if it returns 200 colors, the green line if it is colored down by the red line.
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 see that my two endpoints are sent back, so in my interface, if the endpoint is status 200, I want to color the background.
<div id="result" class="media text-muted pt-3">
How can I get the urls from my server? Do I need to do another javascript using ajax to call my loadDoc or can I send it directly to the browser?
Thanks in advance for any help. I read the following question, similar to mine, but I could not fully understand what they had in mind. Here
source share