一尘不染

使用node.js获取HTTP标头

node.js

是否有内置的方法通过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.

阅读 349

收藏
2020-07-07

共1个答案

一尘不染

此示例代码应工作:

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();
2020-07-07