如何在HTML上显示节点服务器的运行状况

问题描述 投票:0回答:2

我有两台服务器。一个是在端口8080上运行的server1,另一个是在8081上运行的主应用服务器。现在我想在主应用服务器(8081)上运行的UI(HTML)上展示server1的运行状况。我想显示HTML上的这些元素。

1.Status code of server one.

2.Server is UP Or Down.

3.Response of the server one.

这是我的nodejs代码。

const express = require('express');
const http = require('http');
const fs = require('fs');
const bodyParser = require('body-parser');
const router = express.Router();
const path = require('path');
const ejs = require('ejs');
const app = express();
const server1 = express();



server1.get('/health', function (req, res, next) {
  res.json({health: true});
   res.status(200).end();
  });

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', (req,res) => {
    res.render('index');
    console.log('server two')
  })

server1.listen(8080);
app.listen(8081);

Ajax部分:

var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {   
           if (xmlhttp.status == 200) {
               document.getElementById("#demo1").innerHTML = xmlhttp.responseText;
           }
           else if (xmlhttp.status == 400) {
              alert('There was an error 400');
           }
           else {
               document.getElementById('demo1').innerHTML = 'something else other than 200 was returned';
           }
        }
    };

    xmlhttp.open("GET", "http://localhost:8080/health", true);
    xmlhttp.send();

HTML:

<div id="demo1"></div>

我应该怎样做才能在UI上显示server1的运行状况。

node.js health-monitoring
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.