tags:

views:

210

answers:

2

I'm trying to get a node.js server to send css files. I'm modifying this server here:

http://github.com/LearnBoost/Socket.IO-node/blob/master/test/server.js

What's wrong with what I'm doing:

server = http.createServer(function(req, res){
    // your normal server code
    var path = url.parse(req.url).pathname;
    switch (path){
        case '/':
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write('<h1>Welcome. Try the <a href="/chat.html">chat</a> example.</h1>');
            res.end();
            break;

        default:
            if (/\.(js|html|swf)$/.test(path)){
                try {
                    var swf = path.substr(-4) === '.swf';
                    res.writeHead(200, {'Content-Type': swf ? 'application/x-shockwave-flash' : ('text/' + (path.substr(-3) === '.js' ? 'javascript' : 'html'))});
                    res.write(fs.readFileSync(__dirname + path, swf ? 'binary' : 'utf8'), swf ? 'binary' : 'utf8');
                    res.end();
                } catch(e){ 
                    send404(res); 
                }               
                break;
            }
            else if (/\.(css)$/.test(path)){
                    res.writeHead(200, {'Content-Type': 'text/css'});
                    res.end();
                    break;
            }

            send404(res);
            break;
    }
});

Thanks.

A: 

You're only writing a response header for css requests, for one.

I'd imagine if you call curl -I http://your-server/some-file.css, you'd get back a 200 status with a Content-Length of 0. You could just get away with:

res.write(fs.readFileSync(__dirname + path, 'utf8'));

But a.) Don't Repeat Yourself, and b.) the 'sync' in both of those methods means synchronous. It probably isn't for this version, but in Node in general, you should be just calling readFile and passing a callback to finish the request later on. The API isn't great for directly linking, but the File System section should help, look for 'fs.readFile'.

Marc Bollinger
what else do i need to do?
jsnoob
you need to add content, of course.
no
Thanks a lot. I don't understand how to use readFile though. I understand how to use callbacks, but I don't understand what you mean by finish the request.
jsnoob
So, part of the idea of Node is that when you want to do slow things like access the filesystem, you just provide it a callback you want to fire when it's finished and keep on moving. It can take some getting used to, and usually for developing/testing/etc. it's totally fine to use the synchronous methods that will wait for the disk, but you want to transition to the evented model, eventually. See: http://techno-weenie.net/2010/1/15/node-js-for-my-tiny-ruby-brain/
Marc Bollinger
thanks, great read.
jsnoob
A: 

You forgot to send the file.

...
else if (/\.(css)$/.test(path)){
    res.writeHead(200, {'Content-Type': 'text/css'});
    res.write(fs.readFileSync(__dirname + path, 'utf8')); // <--- add this line
    res.end();
    break;
}
BC