title: serving image using nodejs
body: <p>I have a image pic.jpeg which I have to display on browser.Here is snippet of code I have written.</p>
  
  <pre><code>var mimeTypes = {
              '.js' : 'text/javascript',
              '.css' : 'text/css',
              '.gif' : 'image/gif',
              '.jpeg': 'image/jpeg',
              '.html': 'text/html'
          };
  
      contenttype = mimeTypes[path.extname(req.url)];    
      pathname = "." + req.url;
  
  var stream = fs.createReadStream(pathname);
          stream.on('error', function(error) {
              res.writeHead(500);
              res.end();
              return;
          });
              res.setHeader('Content-Type', contenttype);
              res.writeHead(200);
              stream.on('open', function () {
                  // This just pipes the read stream to the response object (which goes to the client)
                  util.pump(stream, res, function(error) {
                  //Only called when the res is closed or an error occurs
                  console.log("Error:");
                  res.end();
                  return;
                  });
              });
  </code></pre>
  
  <p>The above code works most of the time and the images displays like it should, but at times the image is missing.</p>