function http_get (ip, port) {
let WAN = false;
//http.ClientRequest
var data = { //
msg: "can you hear me?",
time: new Date().getTime()
};
var content = qs.stringify(data);
var options = {
host : ip,
port : port,
path : "/",
method : "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
};
var request = http.request(options, function(res) {
console.log("STATUS: " + res.statusCode);
if (res.statusCode == "200") {
WAN = true;
}
console.log(WAN) //true
console.log("HEADERS: " + JSON.stringify(res.headers));
res.setEncoding("utf8");
console.log("get respones");
let data = "";
res.on("data", function (chunk) {
console.log("BODY: " + chunk);
});
res.on("end", function (e) {
console.log("-----end----")
});
});
request.on("error", function (e) {
console.log("problem with request: " + e.message);
});
request.write(content);
request.end(); //
return WAN;
}
let a = http_get("127.0.0.1", 3000)
console.log("a:"+ a); //a:false
print out a:false below and true on top. How can I print out a:true?