简体   繁体   中英

Node.JS images not displaying to clients

I have server.js and client.html. Server.js is running on nodejs and is simply:

var http = require('http'), fs = require('fs');

var app = http.createServer(function(request, response) {
    fs.readFile("client.html", 'utf-8', function(error, data) {
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.write(data);
    response.end();     
    });
}).listen(80);

and then I have client.html which is also very simply just

<img src="/public/images/avatar.gif">

Which just displays as though the image is not valid, I have checked the the director over and over and it is fine, why would it be doing this? I thought it might be because of the headers but text/html should surely display images?

Regards

Matt

It's not displaying the image because for every request (including the image request) it's returning the contents of clients.html.

If you want a static file server, i suggest looking at connect: http://www.senchalabs.org/connect/ or for something simpler, have a look at this: https://gist.github.com/rpflorence/701407

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