Getting HTTP headers with Node.js
Is there a built in way to get the headers of a specific address via node.js?
something like,
var headers = getUrlHeaders("http://stackoverflow.com");
would return
HTTP/1.1 200 OK.
Cache-Control: public, max-age=60.
Content-Type: text/html; charset=utf-8.
Content-Encoding: gzip.
Expires: Sat, 07 May 2011 17:32:38 GMT.
Last-Modified: Sat, 07 May 2011 17:31:38 GMT.
Vary: *.
Date: Sat, 07 May 2011 17:31:37 GMT.
Content-Length: 32516.
Solution 1:
This sample code should work:
var http = require('http');
var options = {method: 'HEAD', host: 'stackoverflow.com', port: 80, path: '/'};
var req = http.request(options, function(res) {
console.log(JSON.stringify(res.headers));
}
);
req.end();
Solution 2:
Try to look at http.get and response headers.
var http = require("http");
var options = {
host: 'stackoverflow.com',
port: 80,
path: '/'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
for(var item in res.headers) {
console.log(item + ": " + res.headers[item]);
}
}).on('error', function(e) {
console.log("Got error: " + e.message);
});