在 node.js Express 框架中设置两个不同的静态目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5973432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up two different static directories in node.js Express framework
提问by sNiCKY
Is it possible? I would like to set up two different directories to serve static files. Let's say /public and /mnt
是否可以?我想设置两个不同的目录来提供静态文件。假设 /public 和 /mnt
回答by facetcounter
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:
您还可以通过指定一个额外的(第一个)参数来设置静态文件将从中提供到网络的路径use():
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.
这样你就可以在网络上得到两个不同的目录来反映你的本地目录,而不是一个在两个本地目录之间进行故障转移的 url 路径。
In other words the URL pattern:
换句话说,URL 模式:
http://your.server.com/public/*
Serves files from the local directory publicwhile:
从本地目录提供文件,public同时:
http://your.server.com/public2/*
Serves files from the local directory public2.
提供本地目录中的文件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
HTH
回答by Randolpho
You can also "merge" directories into a single visible directory
您还可以将目录“合并”到一个可见的目录中
Directory Structure
目录结构
/static/alternate_static
/static/alternate_static
Code
代码
app.use("/static", express.static(__dirname + "/static"));
app.use("/static", express.static(__dirname + "/alternate_static"));
Both static and alternate_static will be served as if they were in the same directory. Watch out for filename clobbers, though.
static 和alternate_static 都将像在同一目录中一样提供服务。不过,请注意文件名破坏。
回答by Phillip Kovalev
It's not possible by one middleware injection, but you can inject staticmiddleware multiple times:
一次中间件注入是不可能的,但您可以static多次注入中间件:
app.configure('development', function(){
app.use(express.static(__dirname + '/public1'));
app.use(express.static(__dirname + '/public2'));
});
Explanation
解释
Look at connect/lib/middleware/static.js#143:
查看connect/lib/middleware/static.js#143:
path = normalize(join(root, path));
There is options.rootis static root, which you define in express.staticor connect.staticcall, and pathis request path.
有一个options.root静态根,您可以在其中定义express.static或connect.static调用,并且path是请求路径。
Look more at connect/lib/middleware/static.js#154:
查看更多connect/lib/middleware/static.js#154:
fs.stat(path, function(err, stat){
// ignore ENOENT
if (err) {
if (fn) return fn(err);
return ('ENOENT' == err.code || 'ENAMETOOLONG' == err.code)
? next()
: next(err);
Path checked only once, and if file not found request passed to next middleware.
路径只检查一次,如果找不到文件,则请求传递给下一个中间件。
Update for Connect 2.x
Connect 2.x 的更新
Links to code are inactual for Connect 2.x, but multiple static middleware usage are still posible as before.
Connect 2.x 的代码链接是不实际的,但仍然可以像以前一样使用多个静态中间件。
回答by Ajay Ruhela
const express = require('express');
const path = require('path');
const pagesPath = path.join(__dirname, '/cheatsheet');
const cssPath = path.join(__dirname, '/stylesheet');
const port = process.env.PORT || 3000;
var app = express();
app.use("/cheatsheet" ,express.static(pagesPath));
app.use("/stylesheet",express.static(cssPath));
app.get('/',(request,response)=>{
response.send('Hello CSS!!!');
});
app.get('/bad',(request,response)=>{
response.send({error: 'Bad Request'});
});
app.listen(port, ()=> {
console.log(`Server is running on Port ${port}` );
console.log(__dirname);
});
});
// folder structure
/cheatsheet/index.html
/stylesheet/style.css

