There's middleware that Express inherits from Connect, which is known as static()
. The function starts a static file server that mounts to a specified path.
// serve files from /static to path /
app.use('/', express.static(__dirname + '/static'));
// server files from /stylesheets to /css
app.use('/css', express.static(__dirname + '/stylesheets'));
A middleware function is a function that runs each time Express receives a request. The static file server will detect if a file exists, and will also detect its MIME type. Therefore stylesheets will be served as stylesheets, scripts as scripts, etc.