最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set dynamic route for static files in express - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Sep 12, 2015 at 23:42 dopatraman asked Sep 12, 2015 at 23:35 dopatramandopatraman 13.9k29 gold badges97 silver badges163 bronze badges 6
  • Clarification -- is there a reason you do not want both paths available for static files? Express allows you to use multiple directories so if you wanted to just have both that also could work? Also what is the issue with your code? To me it seems like it could work. – aug Commented Sep 12, 2015 at 23:40
  • Your if/else statement would work just fine if that satisfies your needs. Keep it outside of the listen function though and in with the rest of your configuration. – James LeClair Commented Sep 12, 2015 at 23:41
  • i need it to be inside the listen function, because the static files returned will differ request to request. – dopatraman Commented Sep 12, 2015 at 23:41
  • @dopatraman please expand on the use case here. If it's route specific this isn't the way to go, but I'm not seeing the whole picture here. – James LeClair Commented Sep 12, 2015 at 23:43
  • based im trying to render a different static html file on a request by request basis. That means if you and i hit the same url, we will see a different static page. – dopatraman Commented Sep 12, 2015 at 23:53
 |  Show 1 more ment

5 Answers 5

Reset to default 3

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);
});

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

If you want to keep all the functionality that es 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);
});

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);
    }
});

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 :)

Based on your last ment: 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 mon 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

发布评论

评论列表(0)

  1. 暂无评论