2013-04-21 18 views
3

在Express.js中,我想將我的圖像/視頻保留在Express.js項目文件夾之外,意思是不在公共/圖像。舉例來說,現在我的媒體是在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

你嘗試'app.use(表達。靜態('/ home/myName/allMyMedia /'));' – Plato 2013-04-21 15:21:27

+0

你也可以嘗試刪除'public/images /',而不是創建一個符號鏈接指向'public/images - >/home/myName/allMyMedia' – Plato 2013-04-21 15:23:12

回答

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.