我使用的是hapi v17.1,.I不是一个专业的程序员。为了进行服务器端图像验证,我需要在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中实现这一点?
发布于 2018-12-07 11:20:18
对于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);
};https://stackoverflow.com/questions/53666993
复制相似问题