在node.js Express框架中设置两个不同的静态目录 [英] Setting up two different static directories in node.js Express framework

查看:181
本文介绍了在node.js Express框架中设置两个不同的静态目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以吗?我想设置两个不同的目录来提供静态文件。让我们说/ public和/ mnt

解决方案

您还可以通过指定将静态文件提供给Web的路径一个额外的(第一个)参数到 use()像这样:

  app.use(/ public,express.static(__ dirname +/ public)); 
app.use(/ public2,express.static(__ dirname +/ public2));

这样你就可以在Web上找到两个不同的目录来镜像你的本地目录,而不是一个url路径



换句话说,URL模式:

  http://your.server.com/public/* 

从本地提供文件目录 public while:

  http://your.server.com / public2 / * 

从本地目录 public2



如果您不希望静态从服务器的根目录提供文件,而是从更合格的路径,这也很有用。 / p>

HTH


Is it possible? I would like to set up two different directories to serve static files. Let's say /public and /mnt

解决方案

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"));

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.

In other words the URL pattern:

http://your.server.com/public/*

Serves files from the local directory public while:

http://your.server.com/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆