I am trying to make a webserver in node.js that downloads an image from Wikipedia and servers it on a page. I cant get it to work. I pasted my code in an online sandbox: http://runnable.com/UXWTyD3pTQ1RAADe.
Heres my code:
var http = require('http');
var fs = require('fs');
var fd = fs.open('name.jpeg', 'r+');
var options = {
    host:'upload.wikimedia.org',
    port:80,
    path:'/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg'
};
var server = http.createServer(function(req, res){
    res.writeHead(200, ['Content-Type', 'text/html']);
    http.get(options,function(res) {
      res.on('data', function (chunk) {
        fs.write(fd, chunk, 0, chunk.length, 0, null);
      });
      res.on('end',function(){
        fd.end();
        res.send("<img src='name.jpeg'></img>");
        res.end();
      });
    });
});
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
I keep running into:
node server.js
Running...
fs.js:415                                                                                    
  binding.write(fd, buffer, offset, length, position, wrapper);                              
          ^                                                                                  
TypeError: Bad argument                                                                      
    at Object.fs.write (fs.js:415:11)                                                        
    at IncomingMessage.<anonymous> (server.js:18:12)                                         
    at IncomingMessage.EventEmitter.emit (events.js:96:17)                                   
    at IncomingMessage._emitData (http.js:359:10)                                            
    at HTTPParser.parserOnBody [as onBody] (http.js:123:21)                                  
    at Socket.socketOnData [as ondata] (http.js:1485:20)                                     
    at TCP.onread (net.js:404:27)   
				
				Working code - saving image file:
/**Try to get an image from Wikipedia and return it**/
var http = require('http');
var fs = require('fs');
var options = {
  host:'upload.wikimedia.org',
  port:80,
  path:'/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg'
};
var server = http.createServer(function(req, res){
  res.writeHead(200, ['Content-Type', 'text/html']);
  http.get(options,function(imgRes) {
    imgRes.pipe(fs.createWriteStream('name.jpeg'));
    res.end("<html><img src='name.jpeg'></img></html>");
  });
});
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
You would also need node-static (http://www.sitepoint.com/serving-static-files-with-node-js/) for serving static file name.jpeg.
But the other way is to do it manually:
var http = require('http');
var fs = require('fs');
var options = {
  host:'upload.wikimedia.org',
  port:80,
  path:'/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg'
};
var server = http.createServer(function(req, res){
  if(req.url == '/name.jpeg') {
    res.writeHead(200, ['Content-Type', 'image/jpg']);
    try {
      var imgData = fs.readFileSync('name.jpeg');
      res.end(fs.readFileSync('name.jpeg'));
    } catch(err) {
      res.end();
    }
  }
  else {
    res.writeHead(200, ['Content-Type', 'text/html']);
    http.get(options,function(imgRes) {
      imgRes.pipe(fs.createWriteStream('name.jpeg'));
      res.end("<html><img src='name.jpeg'></img></html>");
    });
  }
});
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);