Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js copy remote file to server

Right now I'm using this script in PHP. I pass it the image and size (large/medium/small) and if it's on my server it returns the link, otherwise it copies it from a remote server then returns the local link.

function getImage ($img, $size) {
    if (@filesize("./images/".$size."/".$img.".jpg")) {
        return './images/'.$size.'/'.$img.'.jpg';
    } else {
        copy('http://www.othersite.com/images/'.$size.'/'.$img.'.jpg', './images/'.$size.'/'.$img.'.jpg');
        return './images/'.$size.'/'.$img.'.jpg';
    }
}

It works fine, but I'm trying to do the same thing in Node.js and I can't seem to figure it out. The filesystem seems to be unable to interact with any remote servers so I'm wondering if I'm just messing something up, or if it can't be done natively and a module will be required.

Anyone know of a way in Node.js?

like image 790
A Wizard Did It Avatar asked Nov 13 '10 18:11

A Wizard Did It


2 Answers

You should check out http.Client and http.ClientResponse. Using those you can make a request to the remote server and write out the response to a local file using fs.WriteStream.

Something like this:

var http = require('http');
var fs = require('fs');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/',
  {'host': 'www.google.com'});
request.end();
out = fs.createWriteStream('out');
request.on('response', function (response) {
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    out.write(chunk);
  });
});

I haven't tested that, and I'm not sure it'll work out of the box. But I hope it'll guide you to what you need.

like image 149
Juan Avatar answered Oct 21 '22 06:10

Juan


To give a more updated version (as the most recent answer is 4 years old, and http.createClient is now deprecated), here is a solution using the request method:

var fs = require('fs');
var request = require('request');
function getImage (img, size, filesize) {
    var imgPath = size + '/' + img + '.jpg';
    if (filesize) {
        return './images/' + imgPath;
    } else {
        request('http://www.othersite.com/images/' + imgPath).pipe(fs.createWriteStream('./images/' + imgPath))
        return './images/' + imgPath;
    }
}
like image 29
Rooke Avatar answered Oct 21 '22 06:10

Rooke