El servidor Node.js da problemas con scripts e imágenes

I just started using node js and I've moved one of my websites on it:

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

var app = http.createServer(function (req, res) {

    fs.readFile('./index.html', 'utf-8', function(error, content) {
        res.writeHead(200, {'Content-Type' : 'text/html'});
        res.end(content);
    });
});
app.listen(8080);

The index.html is my website home page. With only html it works, but if i put tags in it (for including jquery for example), it gives JS errors in firebug : Uncaught syntax error : unexpected token < in jquery.js, and then of course '$ is undefined'. It doesn't load images either.

I don't really need to do some routing or use Express framework or anything, it's just a simple one-page website. What am I doing wrong ?

preguntado el 22 de septiembre de 12 a las 18:09

1 Respuestas

Your server isn't handling requests for images or other resources. All requests are given the same response of the ./index.html .

This means that if an external script or an image is included in the page, when a request is made by the browser for those resources, the original index.html page will be delivered instead.

NodeJS is fairly low-level. You need to set up your server to manually handle requests for different types of resources based on the URL for each request.

Your best bet will be to read through some NodeJS tutorials. They should cover the basics of serving content, though many of them won't deal with the lower-level details, and will suggest packages like Connect or Express.

Change your code to this, and you'll see all the resources being requested.

var http    =   require('http');
var fs      =   require('fs');
var url     =   require('url');
var path    =   require('path');

var app = http.createServer(function (req, res) {

    var pathname = url.parse(req.url).pathname;
    var ext      = path.extname(pathname).toLowerCase();

    console.log(pathname);

    if (ext === ".html") {
        fs.readFile('./index.html', 'utf-8', function(error, content) {
            res.writeHead(200, {'Content-Type' : 'text/html'});
            res.end(content);
        });
    }
});
app.listen(8080);

Respondido el 22 de Septiembre de 12 a las 19:09

Hello, thank for your help; but with your code it load for a while then browser sends me : 324 (net::ERR_EMPTY_RESPONSE): I already read a very nice beginner guide on node but it didn't cover this inclusion thing. I don't really want to use frameworks or such at least for the moment, because it is a one page website as I said, so I try to keep it light and simple. - Rayjax

@user1397271: It doesn't load because the code above is not sending a response for the image and script resources. The point was to show that there are more requests taking place than just the page request. You need to learn to serve all the content that will be requested. If a request comes for "/JS/my_js_file.js", then you need to serve that file from the file system, or send a 404 if it doesn't exist. Same goes for all requested resources. - Odio a los perezosos

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.