-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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
Comments
Can you elaborate "require the new version number and changes what static files are served"? Maybe with an example scenario. |
I'm using webpack to compile the files to a static folder and serving them using 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. |
If you want to dynamically/logically serve files you can use 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.
and clients make the request like:
|
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 |
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? |
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. |
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. |
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. |
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 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
} |
Closing to move to support forum. |
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.
The text was updated successfully, but these errors were encountered: