Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. For instance, right now my media is in the Express.js project folder:

/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(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. 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.

like image 296
FranXh Avatar asked Dec 26 '22 05:12

FranXh


1 Answers

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)

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.
like image 128
Plato Avatar answered Dec 31 '22 10:12

Plato