使用 node.js 获取 HTTP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5922842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting HTTP headers with node.js
提问by lostsource
Is there a built in way to get the headers of a specific address via node.js?
是否有通过 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.
回答by clee
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();
回答by yojimbo87
Try to look at http.getand 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);
});
回答by Nemo
Using the excellent requestmodule:
使用优秀request模块:
var request = require('request');
request("http://stackoverflow.com", {method: 'HEAD'}, function (err, res, body){
console.log(res.headers);
});
You can change the method to GETif you wish, but using HEADwill save you from getting the entire response body if you only wish to look at the headers.
GET如果您愿意,您可以将方法更改为,但HEAD如果您只想查看标题,则使用可以避免获取整个响应正文。
回答by Dirty Henry
Here is my contribution, that deals with any URL using http or https, and use Promises.
这是我的贡献,它使用 http 或 https 处理任何 URL,并使用 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)
})
回答by Frank Roth
I had some problems with http.get; so I switched to the lib request:
我有一些问题http.get; 所以我切换到 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);
}
}
);
回答by Matt Ball
I'm not sure how you might do this with Node, but the general idea would be to send an HTTP HEADrequest to the URL you're interested in.
我不确定您如何使用 Node 执行此操作,但一般的想法是向您感兴趣的 URL发送HTTP HEAD请求。
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.
头
请求与对应于 GET 请求的响应相同的响应,但没有响应正文。这对于检索写入响应标头中的元信息很有用,而无需传输整个内容。
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);
});
});
});

