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开发者_如何学Go-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.
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();
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);
});
Using the excellent request
module:
var request = require('request');
request("http://stackoverflow.com", {method: 'HEAD'}, function (err, res, body){
console.log(res.headers);
});
You can change the method to GET
if you wish, but using HEAD
will save you from getting the entire response body if you only wish to look at the headers.
Here is my contribution, that deals with any URL using http or https, and use Promises.
const http = require('http')
const https = require('https')
const url = require('url')
function getHeaders(myURL) {
const parsedURL = url.parse(myURL)
const options = {
protocol: parsedURL.protocol,
hostname: parsedURL.hostname,
method: 'HEAD',
path: parsedURL.path
}
let protocolHandler = (parsedURL.protocol === 'https:' ? https : http)
return new Promise((resolve, reject) => {
let req = protocolHandler.request(options, (res) => {
resolve(res.headers)
})
req.on('error', (e) => {
reject(e)
})
req.end()
})
}
getHeaders(myURL).then((headers) => {
console.log(headers)
})
I'm not sure how you might do this with Node, but the general idea would be to send an HTTP HEAD request to the URL you're interested in.
HEAD
Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
Something like this, based it on this question:
var cli = require('cli');
var http = require('http');
var url = require('url');
cli.parse();
cli.main(function(args, opts) {
this.debug(args[0]);
var siteUrl = url.parse(args[0]);
var site = http.createClient(80, siteUrl.host);
console.log(siteUrl);
var request = site.request('HEAD', siteUrl.pathname, {'host' : siteUrl.host})
request.end();
request.on('response', function(response) {
response.setEncoding('utf8');
console.log('STATUS: ' + response.statusCode);
response.on('data', function(chunk) {
console.log("DATA: " + chunk);
});
});
});
I had some problems with http.get
; so I switched to the lib request
:
var request = require('request');
var url = 'http://blog.mynotiz.de/';
var options = {
url: url,
method: 'HEAD'
};
request(options, function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
if (response.headers['content-length']) {
var file_size = response.headers['content-length'];
console.log(file_size);
}
}
);
精彩评论