0

我正在使用 hapi v17.1。我不是专家级程序员。我需要在 hapi js 中获取图像的分辨率以进行服务器端图像验证。我试过image-size插件

var sizeOf = require('image-size');
var { promisify } = require('util');
var url = require('url');
var https = require('http');


................
// my code 
................


const host = 'http://' + request.info.host + '/';
imageName = host + path;

try {
    var options = url.parse(imageName);

    https.get(options, function (response) {
        var chunks = [];
        response.on('data', function (chunk) {
            chunks.push(chunk);
        }).on('end', function () {
            var buffer = Buffer.concat(chunks);

            console.log("image height and width = ",sizeOf(buffer));

        });
    });

} catch (err) {
    console.log('error occured = ', err);
}

图像大小插件

对于 http 它工作正常,但我不能为 https

当我尝试https url并显示错误时

error occured =  TypeError: https.get is not a function
    at handler (/home/jeslin/projects/hapi/gg-admin/app/controllers/web/advertisement.js:178:31)
    at <anonymous>

我怎样才能实现这个https image url

4

1 回答 1

0

对于https 请求,您应该需要 https 模块require('https'),处理 http 和 https 请求的示例片段供您参考。

var sizeOf = require('image-size');
var https = require('https');
var http = require('http');
var url = require('url');

const host = 'http://picsum.photos/200/300';

const request = (host.indexOf('https') > -1) ? https : http;

try {
    request.get(host, function (response) {
        var chunks = [];
        response.on('data', function (chunk) {
            chunks.push(chunk);
        }).on('end', function () {
            var buffer = Buffer.concat(chunks);

            console.log("image height and width = ",sizeOf(buffer));

        });
    });
} catch (err) {
    console.log('error occured = ', err);
};
于 2018-12-07T11:20:18.967 回答