title: nodejs - How to add image data from file into canvas
body: <p>The following code is supposed to read an image file and then add the file data into a canvas with the help of the Canvas module.</p>
  
  <p>When I run this code I receive the error message <strong>Image is not defined</strong>. Is the image object that I'm trying to initialise from a module that I simply import?</p>
  
  <pre><code>var http = require('http'), fs = require('fs'), 
  Canvas = require('canvas');
  
  http.createServer(function (req, res) {
      fs.readFile(__dirname + '/image.jpg', function(err, data) {
          if (err) throw err;
          img = new Image();
          img.src = data;
          ctx.drawImage(img, 0, 0, img.width / 4, img.height / 4);
  
          res.write('&lt;html&gt;&lt;body&gt;');
          res.write('&lt;img src="' + canvas.toDataURL() + '" /&gt;');
          res.write('&lt;/body&gt;&lt;/html&gt;');
          res.end();
      });
  
  }).listen(8124, "127.0.0.1");
  console.log('Server running at http://127.0.0.1:8124/');
  </code></pre>