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?
To serve static files you can use the middleware provided by Express, just add
app.use('/public', express.static(__dirname));
after your dynamic routes.
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.