使用javascript获取arangodb的状态url

m2xkgtsf  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(153)

我需要得到“http://localhost:8529/_admin/cluster/health“的网址,可以检查作为javascript的arangodb集群的状态。
它由如下js文件组成。

// 20200611141601
// http://203.255.92.193:8529/_admin/cluster/health

{
  "Health": {
    "PRMR-3f6a6958-cef1-4326-a7c6-fb20cd921726": {
      "Timestamp": "2020-06-09T04:32:09Z",
      "SyncStatus": "SERVING",
      "Status": "GOOD",
      "Host": "39f18558358b46eca3504bd0b4ebed26",
      "Version": "3.6.3-1",
      "SyncTime": "2020-06-09T04:32:08Z",
      "Endpoint": "tcp://203.255.92.194:8530",
      "LastAckedTime": "2020-06-09T04:32:08Z",
      "Engine": "rocksdb",
      "ShortName": "DBServer0001",
      "Role": "DBServer",
      "CanBeDeleted": false
    },
    "CRDN-4063e580-4ae0-4b89-ba5a-5b4c5616cb20": {
      "Timestamp": "2020-06-09T12:03:09Z",
      "SyncStatus": "SERVING",
      "Status": "GOOD",
      "Host": "c424ee3654814288904d1f31c257e038",
      "ShortName": "Coordinator0019",
      "Engine": "rocksdb",
      "Version": "3.6.3-1",
      "SyncTime": "2020-06-09T12:03:08Z",
      "LastAckedTime": "2020-06-09T12:03:09Z",
      "Endpoint": "tcp://203.255.92.193:8529",
      "Role": "Coordinator",
      "CanBeDeleted": false

我试过了,但没有定义。

const url = require('url');
const address = 'http://localhost:8529/_admin/cluster/health';
const parsedData= url.parse(address, true);

console.log(parsedData.Health); 
console.log(parsedData.Status); 
console.log(parsedData.search); 

const queryData = parsedData.query; 
console.log(queryData.Health);

我不知道怎么做。有没有一个库函数可以用javascript得到“http://localhost:8529/_admin/cluster/health

5t7ly7z5

5t7ly7z51#

url.parse()仅解析您传递给它的URL字符串:

console.log(parsedData);

/*
Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'localhost:8529',
  port: '8529',
  hostname: 'localhost',
  hash: null,
  search: null,
  query: [Object: null prototype] {},
  pathname: '/_admin/cluster/health',
  path: '/_admin/cluster/health',
  href: 'http://localhost:8529/_admin/cluster/health'
}
*/

您需要向服务器发出实际请求,例如:

const http = require("http");

http.get("http://localhost:8529/_admin/cluster/health", res => {
    let data = "";

    res.on("data", chunk => {
        data += chunk;
    });

    res.on("end", () => {
        try {
            const parsedData = JSON.parse(data);
            if (parsedData.error) {
                throw new Error("Server replied with error code " + parsedData.code);
            }
            console.log("Status: Node ID");
            for (node in parsedData.Health) {
                console.log(parsedData.Health[node].Status + ": " + node);
            }
        } catch(err) {
            console.log("Error: " + err.message);
        }
    });

    res.on("error", err => {
        console.log("Error: " + err.message);
    });
});

该示例假定协调器节点在端口8529上本地运行,并且群集的身份验证已禁用。

相关问题