在 node.js Express 框架中设置两个不同的静态目录 [英] Setting up two different static directories in node.js Express framework
问题描述
有可能吗?我想设置两个不同的目录来提供静态文件.假设/public 和/mnt
Is it possible? I would like to set up two different directories to serve static files. Let's say /public and /mnt
推荐答案
您还可以通过为 use()
use()指定一个额外的(第一个)参数来设置将静态文件提供给 Web 的路径代码>像这样:
You can also set the path that static files will be served to the web from by specifying an additional (first) parameter to use()
like so:
app.use("/public", express.static(__dirname + "/public"));
app.use("/public2", express.static(__dirname + "/public2"));
这样你就可以在网络上得到两个不同的目录来反映你的本地目录,而不是一个在两个本地目录之间进行故障转移的 url 路径.
That way you get two different directories on the web that mirror your local directories, not one url path that fails over between two local directories.
换句话说就是 URL 模式:
In other words the URL pattern:
http://your.server.com/public/*
从本地目录 public
提供文件,同时:
Serves files from the local directory public
while:
http://your.server.com/public2/*
提供本地目录 public2
中的文件.
Serves files from the local directory public2
.
顺便说一句,如果您不希望静态从服务器的根目录提供文件,而是从更合格的路径提供文件,这也很有用.
BTW this is also useful if you don't want static to serve the files from the root of your server but rather from a more qualified path.
HTH
这篇关于在 node.js Express 框架中设置两个不同的静态目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!