[英]How to implement static web server with node.js
I want to use node.js & express as a static server. 我想使用node.js&express作为静态服务器。 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 要提供静态文件,您可以使用Express提供的中间件,只需添加
app.use('/public', express.static(__dirname));
after your dynamic routes. 动态路线之后。
You should not use node.js as a static file server, use nginx instead. 您不应将node.js用作静态文件服务器,而应使用nginx。 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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.