简体   繁体   English

Express.js:将默认的public / images文件夹更改为另一个目录(Express.js文件夹之外)

[英]Express.js: Change the default public/images folder to another directory (outside the Express.js one)

In Express.js, I would like to keep my images/videos outside the Express.js project folder, meaning NOT in public/images. 在Express.js中,我想将我的图像/视频保存在Express.js项目文件夹之外,这意味着不在公共/图像中。 For instance, right now my media is in the Express.js project folder: 例如,现在我的媒体位于Express.js项目文件夹中:

/home/myName/Project/public/images

And I would like my images/videos to be in 而且我希望我的图片/视频能够存在

/home/myName/allMyMedia

My app.configure() function right now looks like this: 我的app.configure()函数现在看起来像这样:

app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(expressValidator);
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));
});

I tried to change the '/public' to '../public' but that did not work. 我试图将'/ public'改为'../public',但这不起作用。 I also played a little bit around with the other path declarations for public, but up to know I could not figure out how to fix this. 我也玩了一些关于公开的其他路径声明,但最多知道我无法弄清楚如何解决这个问题。 Any help would be appreciated. 任何帮助,将不胜感激。

If you want to serve some requests from your media folder and others from your public folder, invoke static twice as shown in https://github.com/visionmedia/express/blob/master/examples/static-files/index.js (has good comments) 如果要从媒体文件夹和公共文件夹中的其他人请求某些请求,请调用static两次,如https://github.com/visionmedia/express/blob/master/examples/static-files/index.js所示(有好评)

var express = require('../..');
var app = express();

app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.static('/home/myName/allMyMedia/'));
app.use(app.router);

app.listen(3000);
// GET /images/foo.jpg will first check ./public/images/foo.jpg,
// then /home/myName/allMyMedia/images/foo.jpg,
// then it will attempt to match a routing function to '/images/foo.jpg'
// As usual, as soon as something sends a response, no further functions are called.
// Reorder the app.use stack to change this order.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM