我需要使用 node.js 获取网页中图像文件的大小。为此我使用了一些 Node 模块,但它给出了所有其他资源的大小,如 js 文件、html 大小等。所以如何获取请求的大小(请求图像文件)? .如何找到图像文件的加载时间?
最佳答案
我假设您正在尝试通过 http 而不是图像尺寸来获取图像的文件大小。 (如有错误请指正)
您可以简单地向资源发送 HEAD 请求并查看 Content-Length header :
var http = require('http');
var req = http.request({
host: 'www.gravatar.com',
port: 80,
path: '/avatar/b9da20552a71c4d3c4c6dc2d55418cf2',
method: 'HEAD' },
function(res) {
console.log("Size: " + res.headers['content-length']);
}
).end();
关于node.js - 如何使用 node.js 获取图像文件的字节大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665965/