check image resolution in hapijs

105 Views Asked by At

I am using hapi v17.1 .I am not an expert programmer. I need to get the resolution of an image in hapi js for server side image validation . I have tried image-size plugin

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);
}

image-size plugin

for http it is working fine but I cant do it for https

when I tried for https url and showing the error

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>

how can I implement this for https image url

1

There are 1 best solutions below

0
On

For https request you should require https module require('https'), Sample snippet to handle http & https request for your reference.

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);
};