Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Working code - saving image file:</p> <pre><code>/**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("&lt;html&gt;&lt;img src='name.jpeg'&gt;&lt;/img&gt;&lt;/html&gt;"); }); }); server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP); </code></pre> <p>You would also need node-static (<a href="http://www.sitepoint.com/serving-static-files-with-node-js/" rel="nofollow">http://www.sitepoint.com/serving-static-files-with-node-js/</a>) for serving static file <code>name.jpeg</code>.</p> <p><em><strong>But the other way is to do it manually:</em></strong></p> <pre><code>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("&lt;html&gt;&lt;img src='name.jpeg'&gt;&lt;/img&gt;&lt;/html&gt;"); }); } }); server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload