在nodejs http请求中获取响应"ContentType"

Nat*_*han 7 node.js

我想通过http下载文件并检查"ContentType"响应头.我的下载看起来像这样:

var fileUrl = "<url>";
var request = https.get(fileUrl, function (res) {
res.on('data', function (data) {
    //...
});
res.on('error', function (error) {
    //...;
});
Run Code Online (Sandbox Code Playgroud)

我得到了数据,但有没有办法访问内容类型的共振头?

rob*_*lep 13

res变量是一个实例http.IncomingMessage,其实例headers包含标题:

var request = https.get(fileUrl, function (res) {
  var contentType = res.headers['content-type'];
  ...
});
Run Code Online (Sandbox Code Playgroud)