Node.js connect not serving images correctly

I am currently trying to write a very simple webserver using Node.js and connect, that basically just serves a single directory staticly.

var server = connect.createServer();
server.use(connect.logger());
server.use(connect.static(DOCUMENT_ROOT));
server.listen(SERVER_PORT);

This is basically working, I can connect to the server and its serving the index.html to me.

The problem occurs when I try to serve images (either in an html or by opening their url directly). They are served, but it seems not completely, yielding a result such as this:

I looked into the headers, but can't find any obvious flaws. The correct content-size is there, the only thing I find rather strange is that the content-type seems to be missing, but it seems the browser figured it out correctly. Interestingly, if I serve the image manually, it works. So using this code the image is served correctly:

var server = connect.createServer();
server.use(connect.logger());

server.use(function(req, res, next) {
  console.log(req.url);

  if (req.url != "/testimage.png") {
    next();
    return;
  }

  var data = fs.readFileSync(DOCUMENT_ROOT + req.url);

  res.writeHead(200, {'Content-Type': 'image/png'});
  res.write(data);
  res.end();
});

server.use(connect.static(DOCUMENT_ROOT));
server.listen(SERVER_PORT);

Any ideas why serving the images with connect does not work? Since this is a rather small example I find it strange its not working... . Did I understand something wrong about connect?

Update

I tracked it down a little further. It seems to be an issue with the streaming component of the send module - it never stops streaming the file but repeats it endlessly. I think this might be related to me running Nodelike so I opened an issue at the Nodelike repo. If someone has an idea I'd still be glad

Answers


Just to solve this: It was a problem with Nodelike after all. It seems Nodelike has a problem or bug with streaming that causes this. Switching to a synchronized delivery of the files solved the issue.


Need Your Help

Append content on random location with jQuery?

javascript jquery random appendto

I need to display a list of items in a page, and this page is updated with AJAX. Every time a new item is pushed to the client I want it to appear at a random location inside my ul list.