dopatraman
dopatraman

Reputation: 13908

How to set dynamic route for static files in express

Currently, to serve static files im doing something like this:

app.use('/', express.static(__dirname + '/public/'));
// then i start the server
http.createServer(app).listen(port, function() {
    console.log('HTTP Express server listening on port %s', port);
});

However, this sets the same directory for that path for every request, under all conditions. What I want to do is something like this that varies the response request to request:

http.createServer(app).listen(port, function() {
    if (someCondition) {
        app.use('/', express.static(__dirname + '/public/'));
    }
    else {
        app.use('/', express.static(__dirname + '/public/someotherpath'));    
    }
    console.log('HTTP Express server listening on port %s', port);
});

How can I do this?

Upvotes: 3

Views: 5561

Answers (5)

Sudhan
Sudhan

Reputation: 341

I also had that same problem now i fixed it by making a condition just like the below code

app.use("/", (req, res, next) => {
  //check a condition if its false then return
  if(1 !== 1)return
  //After returned all conditions
  next()
}, express.static("public"))

And this code checks if 1 is not 1 then wont show the files if 1 is 1 then it shows all public files :)

Upvotes: 1

prograhammer
prograhammer

Reputation: 20590

Old question...but this is a quick solution...

If you want to keep all the functionality that comes with express.static, then you can just monkey-patch req.url (since it's just middleware):

const path = require('path');
const express = require('express');
const app = express();

// Dynamic path, but only match asset at specific segment.
app.use('/website/:foo/:bar/:asset', (req, res, next) => {
  req.url = req.params.asset;
  express.static(__dirname + '/static')(req, res, next);
});         

// Just the asset.
app.use('/website/*', (req, res, next) => {
  req.url = path.basename(req.originalUrl);
  express.static(__dirname + '/static')(req, res, next);
});

Upvotes: 2

Akxe
Akxe

Reputation: 11485

You do it the other way around you modify url to make your desire effect

app.use('/', function(req,res,next){
    if(condition){
        req.url = newURL // add something to lead to different directory
    }
    next();
});

app.use('/', express.static(__dirname + '/public/'));
// then i start the server
http.createServer(app).listen(port, function() {
    console.log('HTTP Express server listening on port %s', port);
});

Upvotes: 3

Ofer Segev
Ofer Segev

Reputation: 5282

the result of express.static() is a middleware function, so you can call it dynamically with your condition.

app.get('/', (req, res, next) => {
    if (condition) {
        express.static(__dirname + '/public/')(req, res, next);
    } else {
        express.static(__dirname + '/public/someotherpath')(req, res, next);
    }
});

Upvotes: 1

James LeClair
James LeClair

Reputation: 1294

Based on your last comment: you can still do this within the router

app.get('/somepath', function(req, res){
    if(req.someCondition){
      res.sendFile('./path/to/appropriate/file.html');
    } else {
      res.sendFile('./path/to/different/file.html');
    }
}

Leave express.static to serve your common files like js, css, and images. Send different html files using the .sendFile() method. This avoids having to use the renderer if you prefer not to use something like jade or ejs

Upvotes: 0

Related Questions