有没有一种内置的方法可以通过node.js获取特定地址的报头?
像这样,
var headers = getUrlHeaders("http://stackoverflow.com");
将会返回
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.
发布于 2011-05-14 19:23:29
下面的示例代码应该可以工作:
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();
发布于 2011-05-08 02:08:12
试着看看http.get和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);
});
发布于 2014-02-26 13:52:46
使用优秀的request
模块:
var request = require('request');
request("http://stackoverflow.com", {method: 'HEAD'}, function (err, res, body){
console.log(res.headers);
});
如果愿意,可以将方法更改为GET
,但是如果只希望查看头部,则使用HEAD
可以省去获取整个响应正文的麻烦。
https://stackoverflow.com/questions/5922842
复制相似问题