Cacomania: Serving static files with Node.js

Cacomania

Serving static files with Node.js

Guido Krömer - 8. November 2012 - Tags: ,

Node.js isn't built for serving static files, but during testing and development it's quite handy doing this without configuring an apache or whatever http server. No configuration file is needed just two lines of JavaScript to get this server running.

This minimalistic http server does not support caching, gzip or whatever, it just delivers the most common files like style sheets or images. Use the code from the gist below:

The usage is really simple just define the document root folder, the port that is 8000 by default, an ip address to listen on and the directory index if it's not index.html.

Other MIME types can be added using the addMime method.

Don't forget this HTTP server is just a helper during development, don't use this in production environment!