I want to use node.js & express as a static server. I want it to serve all views without having to explicitly define routes. How do I do this?
2 Answers
You should not use node.js as a static file server, use nginx instead. But if you have to or you are just playing with node, you can use this (coffeescript):
Handle static files:
app.use '/public/', express.static(__dirname + '/public')
Example:
<img src="/public/logo.jpg" alt="" />
Handle single app page:
app.get '/', (req, res)->
res.sendfile './public/index.html'
app.use(express.static(__dirname));and the middleware takes care of all URL's that match a static resource.