Skip to content Skip to sidebar Skip to footer

Style Sheet - Not Loading With Web Page

I'm running a local copy of nodejs and have taken some code which runs a web service and references a local index.html on startup. This service is running locally on my desktop. I'

Solution 1:

Your server:

http.createServer(function (req, res){
   fs.readFile('index.html'

… is configured to ignore everything in the request and always return the contents of index.html.

So when the browser requests the stylesheet it is given index.html.

You need to pay attention to the path (which will be in the request) and serve up appropriate content (which for serving up static files means mapping it onto the file system, determining if the file exists or not, then loading that file or a 404 message), with the appropriate content type (so you'd need to determine that for each kind of file), and status code.

This is a significant chunk of work and not worth reinventing the wheel over so it would probably be best done by finding a static file serving module (Google turns up node-static) for Node (or replacing Node with something like Lighttpd or Apache HTTPD).

If you want to serve up dynamic content as well as static content, then Express is a popular choice (and has support for static files).

Post a Comment for "Style Sheet - Not Loading With Web Page"