Can't download files using nodejs

nodejs

#1

I have this code in one of my projects. The problem is that it isn’t downloading any file, don’t get me wrong, it works in my own pc but in my workspace isn’t working, I wonder if I’m not allowed to download or something like that. I would really appreciate any help.

function downloadFile(url, dest, cb) {
    // first check if file doesn't already exist
    fs.stat(dest, function (err, stat) {
        if (!(err == null) && err.code === 'ENOENT') {
            var file = fs.createWriteStream(dest);
            https.request(url, function (response) {
                response.pipe(file);
                file.on('finish', function () {
                    file.close(cb);
                });
            }).on('error', function (err) {
                fs.unlink(dest);
                if (cb) {
                    cb(err.message);
                } else {
                    console.log(err);
                }
            });
        }
    });
}

#2

There is nothing in cloud9 setup that could block the above code snippet.
Check the url and add some more console.log calls to see why it fails.
You can also use the built in nodejs debugger


#3

alright, I still wonder why it fails in cloud9 but it doesn’t in my pc :confused:
I will try to do what you said, will updated if something failed…


#4

Ok, I think I know where the problem is… I’m getting 2 completely different html content in c9 and in my pc, how is this even possible???
Do c9 send some kind of headers or something? weird I must say :'c
Well then, If this continues I guess I will have to make a branch only for cloud9


#5

c9 doesn’t modify contents of downloaded files, from what url are you trying to download?