-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_server.js
32 lines (23 loc) · 916 Bytes
/
find_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const https = require("https");
const request = require("request")
exports.findServer=function(serverArray){
//search through an array of servers for the highest priority server
// (lowest priority number) that is available.
// return a promise that either responds with the server information
// or rejects with an error message
const url = serverArray[0].url;
return new Promise((resolve, reject) => {
// search for servers, the result of which will either:
//
//resolve("I found one!"); // fulfilled
// or
//reject("No server found"); // rejected
request.get(url, (error, response, body) => {
let json = JSON.parse(body);
if(error){
return reject(error);
}
return resolve(body);
}); //end of request
}); //end of promise construction
}