Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve static files based on changing config file at runtime #3165

Closed
DanielLuu opened this issue Jan 3, 2017 · 10 comments
Closed

Serve static files based on changing config file at runtime #3165

DanielLuu opened this issue Jan 3, 2017 · 10 comments

Comments

@DanielLuu
Copy link

Is it possible to make a get/post request that forces the server to require the new version number and changes what static files are served? This is for a live versioning/feature toggling aspect of my application. Or is there a better more standard way of doing feature toggling.

@hacksparrow
Copy link
Member

Can you elaborate "require the new version number and changes what static files are served"? Maybe with an example scenario.

@DanielLuu
Copy link
Author

I'm using webpack to compile the files to a static folder and serving them using app.use(express.static(publicPath));

So what I want to be able to do is load a new folder name from a file or database (ex. v1.0 vs v1.1) and point to that new folder location. That way I can easily switch between multiple versions of my UI.

@hacksparrow
Copy link
Member

If you want to dynamically/logically serve files you can use res.sendFile.

However, it is best to let a front facing proxy such as nginx to serve static assets or use a CDN. This spares Express from reading from the file system, which can become computing intensive as the traffic grows.

You could create directories for each version of the UI.

ui/1.0
ui/1.1

and clients make the request like:

https://example.com/ui/1.0/index.js
https://example.com/ui/1.0/style.css
https://example.com/ui/1.1/index.js
https://example.com/ui/1.1/style.css

@wesleytodd
Copy link
Member

wesleytodd commented Jan 3, 2017

One thing to note:

Depending on the scale of your app complexity, and the scale of your user base, you might consider NOT using node to serve your static files. Typically more complex projects have more needs, and using something like nginx is better suited for that (faster, built-in caching, built-in gzip, ect).

That being said, we use something very similar to what @hacksparrow outlines. We also serve it from a separate domain which we put behind nginx and a cdn.

EDIT: I think built in http2 for static files as well, but you would have to look that one up

@DanielLuu
Copy link
Author

Do either of you have experience serving different files to the main route of an nginx server based on database retrieval? Or have any idea where to get started with that?

@wesleytodd
Copy link
Member

wesleytodd commented Jan 3, 2017

We do this at my company. We put it behind a cdn, and then issue the cdn a 301 redirect to a file served directly from nginx. That way the node layer is fast and efficient, and the cdn cached the redirect so it doesn't request the server ever again.

@DanielLuu
Copy link
Author

Yeah wondering if i can get the CDN to point to a different index.html file based on an external config file. Not sure if nginx has that capability.

@wesleytodd
Copy link
Member

That would depend on the cdn I guess. Nginx probably can't do that natively without a plugin or scripting, but doing the 301 technique is really easy and a decent solution to the problem with express.

@dougwilson
Copy link
Contributor

I would suggest that unless this is a bug report or issue with Express, we should probably move the discussion to the support area https://gitter.im/expressjs/express

That said, I would like to add my thoughts here as well :)

From the conversation above, I believe I understand what you're trying to accomplish, which sounds like you want to basically have a app.use(express.static(path)) and then at runtime, be able to alter that path variable to cause static files to switch to another version of your assets. Without getting into the different merits of all the different pros and cons of versioning assets, managing, and switching between them, I'll just focus on a very simple implementation of this request in Express:

var dyn = createDynStatic(path)
app.use(dyn) // setup your static here

// you would POST /switch/1.0
// to start serving from public/1.0/
app.post('/switch/:version', function (req, res) {
  dyn.setPath('public/' + req.params.version)
  res.sendStatus(200)
})

function createDynStatic (path) {
  var static = express.static(path)
  var dyn = function (req, res, next) {
    return static(req, res, next)
  }
  dyn.setPath = function (newPath) {
    static = express.static(newPath)
  }
  return dyn
}

@dougwilson
Copy link
Contributor

Closing to move to support forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants