利用 Express. static 托管靜態文件
1、 如果你的靜態資源存放在多個目錄下面,你可以多次調用 express.static 中間件:
app.use(express.static('public'));
現在, public 目錄下面的文件就可以訪問了。
http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html
2、 如果你希望所有通過 express.static 訪問的文件都存放在一個“虛擬( virtual)”目
錄(即目錄根本不存在)下面,可以通過為靜態資源目錄指定一個掛載路徑的方式來實現,
如下所示:
app.use('/static', express.static('public'));
現在,你就愛可以通過帶有 “/static” 前綴的地址來訪問 public 目錄下
面的文件了。
http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html