简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM