1

I currently have this nodejs code to check if a website server is up or down:

var http = require("http");
http.get({host: "nodejs.org"}, function(res){
   if( res.statusCode == 200 )
       console.log("This site is up and running!");
   else
       console.log("This site is down " + res.statusCode);
});

Now, I want the program to ask the user to enter a website and then it will check if that website is up or down. I tried using the prompt method here:

var website = prompt("Please enter the website server you would like to check", "google.com");

However when I run this, I get this error:

ReferenceError: prompt is not defined
 at Object.<anonymous> (/Users/navania/WebstormProjects/untitled/UpOrDown.js:3:15)
 at Module._compile (internal/modules/cjs/loader.js:776:30)
 at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
 at Module.load (internal/modules/cjs/loader.js:653:32)
 at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
 at Function.Module._load (internal/modules/cjs/loader.js:585:3)
 at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
 at startup (internal/bootstrap/node.js:283:19)
 at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3).
I also tried using window.prompt as many other questions suggested, but that still gave me the same error except it says that window is not defined.

Any help would be appreciated. Thanks!

Pop-A-Stash
  • 6,572
  • 5
  • 28
  • 54
  • @IMustBeSomeone https://www.npmjs.com/package/prompt it would take more than simply installing it... the existing code wouldn't work. – Kevin B Jul 01 '19 at 20:51
  • well, no, it wouldn't break it any more than it's already broken. but it'd still be broken after installing it. That npm package you suggested installing hasn't seen an update in 3 years. – Kevin B Jul 01 '19 at 20:54

2 Answers2

0

In which environment do you want to use this code? If understand your mean properly, you need to get url from command line as an argument. To do so, take a look at this code:

var http = require("http");
var [url] = process.argv.slice(2);
console.log(url);
var request = http.get({ host: url }, function(res){
   if( res.statusCode == 200 || res.statusCode == 301 || res.statusCode == 302)
       console.log("This site is up and running!");
   else
       console.log("This site is down " + res.statusCode);
});
request.on('error', function(err) {
    console.log("This site is down");
});
request.end();
$ node index.js http://nodejs.com

I refer you to this post for more information.

agtabesh
  • 548
  • 3
  • 15
  • Thanks for the reply. I used this code however, when I ran google.com in the terminal command, it says that this website is down (with status code 400) even though its currently up. Any ideas why this may be? In fact it seems that any website I enter returns down with status code 400. – oofmeister27 Jul 03 '19 at 02:20
  • @oofmeister27 it works on my computer. If you want to check other protocol than `http`, you need to use a proper module, for example `https` module for `https` protocol. to get rid of such things, I recommend you to use a full-featured module such as [request](https://github.com/request/request) or [axios](https://github.com/axios/axios). – agtabesh Jul 03 '19 at 07:03
  • Hm, I am currently using web storm, if that changes anything. I do want to only use the protocol http. Could you maybe send some screenshots of what you did on your computer? – oofmeister27 Jul 04 '19 at 03:43
  • I am currently using the command "node UpOrDown.js http://google.com" which then returns "This site is down 400" – oofmeister27 Jul 04 '19 at 04:42
  • @oofmeister27 google.com redirects all `http` request to `https` and consequently, returns status code `301` which is not actually `200`. You need to take status code `301` into account. – agtabesh Jul 04 '19 at 05:54
  • Thanks for the help! it seems to work now. do you have any websites i can test that are down currently? – oofmeister27 Jul 10 '19 at 03:22
  • @oofmeister27 you can use any arbitrary website which is not already exists such as `jkhkfhsdkfshfk.com` ;). If it words, please mark the answer as best answer to help others who have the same problem as yours. – agtabesh Jul 10 '19 at 06:32
  • Hm, thats weird. It says up and running when I enter that website. Beside status codes 200 and 301, are there any other ones which need to be implemented into my code? when i print out the status code for that website, it gives 200... – oofmeister27 Jul 10 '19 at 14:23
  • @oofmeister27 on my computer it throws an error `Error: getaddrinfo ENOTFOUND jkhkfhsdkfshfk.com`. using `200`, `301`, and `302` is enough for your purpose. – agtabesh Jul 10 '19 at 15:05
  • but the thing is my program returns status code 200 for the website jkhkfhsdkfshfk.com, so its obviously going to return that the website is up (even though its not). i do not think adding status code 302 will help fix this issue. Its also weird that you get an error on yours (i am not). could you past the code that you are using? it might just be that the code doesn't work for websites that do not exist. – oofmeister27 Jul 10 '19 at 15:34
  • Are you using exactly the code I shared with you above? Please check it again – agtabesh Jul 10 '19 at 15:36
  • Yes, I am using this code: var http = require("http"); var [url] = process.argv.slice(2); http.get({ host: url }, function(res){ if( res.statusCode == 200 || res.statusCode = 301) console.log("This site is up and running!"); else console.log("This site is down " + res.statusCode); }); – oofmeister27 Jul 10 '19 at 15:38
  • You need to use two equal sign in your condition. Use `res.statusCode == 301` instead of `res.statusCode = 301`! – agtabesh Jul 10 '19 at 15:41
  • i did this. it still does gives that the site is running. I even added status code 302 in my if statement and it still says its up. code: var http = require("http"); var [url] = process.argv.slice(2); http.get({ host: url }, function(res){ if((res.statusCode == 200) || (res.statusCode == 301) || (res.statusCode == 302)) console.log("This site is up and running!"); else console.log("This site is down " + res.statusCode); }); – oofmeister27 Jul 10 '19 at 15:44
  • for some reason, the status code for that website is 200. – oofmeister27 Jul 10 '19 at 15:56
  • ok after trying for some time, i think i am at the same point as you. when I run that website jkhkfhsdkfshfk.com, i get an error: events.js:174 throw er; // Unhandled 'error' event ^ Error: getaddrinfo ENOTFOUND jkhkfhsdkfshfk.com jkhkfhsdkfshfk.com:80 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26) is this what you are getting? – oofmeister27 Jul 10 '19 at 20:49
  • @oofmeister27 yes! I updated the code to handle such error messages. – agtabesh Jul 10 '19 at 23:11
  • Thanks! it works now. marked as best answer. – oofmeister27 Jul 11 '19 at 20:35
-1

Use readline module. It comes with nodejs.

Example from the docs:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What do you think of Node.js? ', (answer) => {
  console.log(`Thank you for your valuable feedback: ${answer}`);
  rl.close();
});
grabantot
  • 2,111
  • 20
  • 31