1
\$\begingroup\$
var fs = require('fs');
const requestHandler = require('../validations/requestHandler');

exports.index = (req, res) => {
    try {
        requestHandler(req);
            var options = {
                'method': 'GET',
                'hostname': 'products.torrent.com',
                'path': '/torrent/product',
                'headers': {
                'accept': 'text/plain',
                'WebServiceToken': '3A4SnowWqg+Xf0TgMoTEZ12B4AJ1='
                },
                'maxRedirects': 20
            };
            var body = null;
            var request = https.request(options, function (result) {
                var chunks = [];
            
                result.on("data", function (chunk) {
                    chunks.push(chunk);
                });
            
                result.on("end", function (chunk) {
                    body = Buffer.concat(chunks);
                    console.log(body.toString());
                });
            
                result.on("error", function (error) {
                    console.error(error);
                });
            });
            
            request.end();
            res.json(body);

    } catch(err) {
        next(err);
    }
}

I don't use ES6, so don't point it out, I don't want to set it up just yet, but I am wondering if we can cut down on some code, because this doesn't seem clean at all. I am not sure if there's a better library for handling requests, because I feel this is way too wordy.

\$\endgroup\$
1
  • \$\begingroup\$ Just FYI: You don't need to "setup" ES6. Node supports most of it out of the box. You can just use it. See node.green \$\endgroup\$
    – RoToRa
    Sep 6 '21 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.