0

I am working on a small project using Node.js.

The objective is to send an HTTP Request to an array of websites and display what they return to me.

First someone helped me to figure out that I needed a specific Node.js module (XMLHttpRequest). So I "required" it after installing it with NPM. Then I instantiate it.

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();

xmlHttp.open( "GET", theUrl, false );

//I don't think I need this
xmlHttp.send(null);

//Log some stuff to the console, namely the "response"
console.log(xmlHttp.responseText);
console.log(xmlHttp.statusText);
console.log("Finished!");

Now I believe what this will do is send a GET message to "theUrl", and then save the response in the xmlHttp Object's responseText member.

So now I should have a response. I should be able to print that as text (console.log(xmlHttp.responseText);). What should be in this response?

I expect to get something like "200 OK" but that is nowhere in the response I get. Am I going about this in the right way?

I plan on using the Async Node.js module to send a request like this to an array of URLs, trim up their response (name of the website name, the response status code, and each of the response headers).

3
  • This should help nodejs.org/api/http.html#http_http_request_options_callback Commented Mar 28, 2014 at 21:45
  • 1
    Rather than trying to simply duplicate browser code, if you are using node, it would be easier to just use the request module. I think you would find it simpler to use. There is a reason it is the #3 most-depended on package on NPM. Also, because of how much it is used, you will have a bigger pool of potential help if you have questions about it. Commented Mar 28, 2014 at 22:27
  • Can you explain this? I am requiring xmlHttpRequest...is there much difference? Commented Mar 29, 2014 at 0:12

1 Answer 1

0

You can use below;

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
    if (this.readyState == 4) {
        // responseText => response body as string
        // status => 200 is OK, 404 page not found
    }
};

xhr.open("GET", "yor_url");
xhr.send();

responseText: response body as string

status: 200 is OK, 404 page not found

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.