5

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

knoxgon
  • 1,070
  • 2
  • 15
  • 31
  • @EndaMolloy Hello I'm having a similar issue as OP, can you elaborate more on how to do this? – user2737948 Mar 03 '18 at 16:02
  • @ user2737948 Do you have any experience using a templating language like handlebars or ejs? Looking at the question again, I see the '/' route is used for rendering so if you decide to use an ajax call you would need a second route. – sharkdawg Mar 03 '18 at 17:28
  • @EndaMolloy No, I dont have any experience in templating actually in my code I dont render anything I only have a get that fetchs information. What I do is fetch that information and I want to fill it an array with that information then send it to the user so he can displays it in an html. Can you give me a hand? – user2737948 Mar 03 '18 at 17:31
  • @ user2737948 Post a new question with your code – sharkdawg Mar 03 '18 at 17:39
  • @EndaMolloy here it is: [link](https://stackoverflow.com/questions/49087364/fill-an-array-based-on-a-get-response-express) – user2737948 Mar 03 '18 at 17:58

1 Answers1

0

Refer to route parameters in the expressjs docs. Use a url as a route parameter. How you return the service status is up to you, you could use express-flash to flash the data to a template. You could use it as an endpoint that can be curled by the client's browser.

Jason
  • 21
  • 4