When I use the http module to GET a .org domain I get a 400 response. (Tried with google.org so it's not a server error.) Is this normal behavior?
var http = require('http');
http.get("google.org", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Your code issues the following HTTP request:
GET google.org HTTP/1.1
Host: localhost
It's your local machine that's responding with a 400 (since the request is indeed invalid). This happens because internally, node uses the url module to parse the string you pass to http.get. url sees the string google.org as a relative path.
url.parse('google.org');
{ protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: 'google.org',
path: 'google.org',
href: 'google.org' }
Since your string parses to a null hostname, node defaults to using localhost.
Try using a fully-qualified URL.
var http = require('http');
http.get("http://google.org", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});