Check if website is contactable

2019-04-28 07:44发布

问题:

I want to check if a specific website is online. The name of the website comes from an input field and is sending via post.

There is an NPM module for ping hosts, but this do not help me so much. I need a solution for checking URL's with params, like: hostname.com/category

I would be grateful for suggestions.

回答1:

Just make an HTTP request.

var http = require('http');
http.get('http://example.com/category', function (res) {
  // If you get here, you have a response.
  // If you want, you can check the status code here to verify that it's `200` or some other `2xx`.

}).on('error', function(e) {
  // Here, an error occurred.  Check `e` for the error.

});;