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

时间:2013-04-21 14:52:18

标签: node.js express

在Express.js中,我想将我的图像/视频保存在Express.js项目文件夹之外,这意味着不在公共/图像中。例如,现在我的媒体位于Express.js项目文件夹中:

/home/myName/Project/public/images

我希望我的图片/视频能够在

/home/myName/allMyMedia

我的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')));
});

我试图将'/ public'更改为'../public',但这不起作用。我也玩了一些关于公开的其他路径声明,但最多知道我无法弄清楚如何解决这个问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:7)

如果您要从媒体文件夹和公共文件夹中的其他人提出某些请求,请按https://github.com/visionmedia/express/blob/master/examples/static-files/index.js中所示的方式调用static两次(有很好的评论)

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.