script to check if status code is not equal to 200

967 views
Skip to first unread message

khinester

unread,
Apr 13, 2012, 7:56:51 AM4/13/12
to nodejs
hello, i have this code:

var http = require('http')
, url = require('url')
, util = require('util')
, exec = require('child_process').exec;

var site = http.createClient(80, "www.domain.tld", false);
var req = site.request("GET", "/");
req.end();
req.on('response', function(res){
if(res.statusCode !== 200){
// launch a new instance
var ec2 = require("ec2")
, fs = require("fs");
// Read in the configuration above.
var configuration =
JSON.parse(fs.readFileSync("configuration.json", "utf8"));
// Create an ec2 function that uses your configuration.
ec2 = ec2(configuration)
// Run an instance and wait for it to become ready.
ec2("RunInstances", {
ImageId: "ami-XXXXXX"
, KeyName: "micros-ie"
, InstanceType: "t1.micro"
, MinCount: 1
, MaxCount: 1
}, function (error, response) {
if (error) {
throw error
}
ready();
});
} else {
res.on('data', function(chunk){
function puts(error, stdout, stderr) { util.puts(stdout) }
exec("my command", puts);
});
}
});

basically, i would like to launch a new EC2 instance if the status
code is not equal to 200. this works ok, but my problem is that when
the code goes into the 'else' loop, i would like to check if the
'site' still returns a 200 status code.

any advice much appreciated

Jann Horn

unread,
Apr 13, 2012, 1:18:17 PM4/13/12
to nod...@googlegroups.com

Put that whole script into a function and use recursion.

Reply all
Reply to author
Forward
0 new messages