I am new to js, on the project that I am working on, I have such a code. it does not output anything to the console. And I have no errors in the console.

If there are no errors can you help to reformat it pls, since it is now poorly readable, it may be clear why this code does not work.

var url = require('url');
var https = require('https');
var sharp = require('sharp');

var hasData = false;
window.run = function(worker) {
    var server = worker.getHTTPServer();
    server.on('request', function(req, res) {
        hasData = false;
        var body = [];
        if (req.method != 'GET') {
            error(res, 400);
        }

        req.on('data', function(chunk) {
            hasData = true;
            body.push(chunk);
        });

        req.on('end', function() {
            parseURI(req.url) 
                .then(function(uriData) {
                    getImage('https://cdn.google.com' + uriData.origUri)
                        .then(function(imageData) {
                            sharp(Buffer.from(imageData.data, 'binary')) 
                                .resize(uriData.resizeWidth, uriData.resizeHeight) 
                                .toBuffer()
                                .then(function(resizedImage) {
                                    var imagemin = require('imagemin');
                                    var imageminPngquant = require('imagemin-pngquant');
                                    imagemin.buffer(resizedImage, {
                                            plugins: [
                                                require('imagemin-mozjpeg')(), 
                                                imageminPngquant(),
                                            ]
                                        })
                                        .then(function(optimazedImage) {
                                            res.writeHead(200, {
                                                'Content-Type': imageData.contentType,
                                                'Content-Length': Buffer.byteLength(optimazedImage, 'binary')
                                            });
                                            res.end(optimazedImage, 'binary');
                                        })
                                        .catch(function(err) {
                                            console.log('Cannot optimize image: ' + req.url)
                                            console.log(err)
                                            res.writeHead(200, { 
                                                'Content-Type': imageData.contentType,
                                                'Content-Length': Buffer.byteLength(resizedImage, 'binary')
                                            });
                                            res.end(resizedImage, 'binary');
                                        });
                                })
                                .catch(function(err) {
                                    console.log('Image Resize failed: ' + req.url);
                                    console.log(err);
                                    error(res, 500);
                                });
                        })
                        .catch(function(err) {
                            console.log('Image Download failed: ' + 'https://cdn.google.com' + uriData.origUri); 
                            error(res, 404);
                        });
                })
                .catch(function(data) {
                    console.log(data);
                    error(res, 400);
                });

        });
    });
};

function parseURI(uri) {
    return new Promise(function(resolve, reject) { 
        var data = {};
        let splitedUri = uri.split('@');
        let resizeParams = splitedUri.pop();
        data['uri'] = uri
        data['resizeWidth'] = +parseInt(resizeParams.split(/x|X/)[0]) || null;
        data['resizeHeight'] = +parseInt(resizeParams.split(/x|X/)[1]) || null;
        data['origUri'] = splitedUri.join('@');
        if (data['resizeWidth'] == null && data['resizeHeight'] == null) {
            reject('Can not parse requested URI ' + uri.toString());
        }
        resolve(data);
    });
}

function getImage(uri) {
    return new Promise(function(resolve, reject) {
        //...
    });

}

function error(res, errcode) {
    //...
}
New contributor
JohnRichards is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

put on hold as off-topic by IEatBagels, Dannnno, Ethan Bierlein, Toby Speight, 200_success Oct 11 at 17:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – IEatBagels, Dannnno, Ethan Bierlein, Toby Speight, 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Welcome to Code Review! What does this code do? Please tell us, and make that the title of the question. See How to Ask. – 200_success Oct 11 at 14:12
  • @200_success I updated my question. It gets Image from url. – JohnRichards Oct 11 at 14:32
  • 2
    Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.". – Sᴀᴍ Onᴇᴌᴀ Oct 11 at 15:30
  • Also: callbackhell.com – Sᴀᴍ Onᴇᴌᴀ Oct 11 at 15:31
  • 1
    If the code doesn't work, it's not ready for refactoring, nor for review. – Toby Speight Oct 11 at 17:04

Browse other questions tagged or ask your own question.