expressjs / serve-static

Serve static files
MIT License
1.38k stars 227 forks source link

add prefixing functionality #91

Closed fooloomanzoo closed 7 years ago

fooloomanzoo commented 7 years ago

Adding support for a prefix function in options to dynamically prefixing the paths. This could be usefull to serve different subfolders statically depeneding on the user-agent or other parts of the request. For example:

var express = require('express')
var serveStatic = require('serve-static')

var app = express()

app.use(serveStatic( resolvePath( '/build', {
  prefix: serveByUserAgent
}));

app.listen(3000)

function serveByUserAgent(req) {
  var userAgent = req.get('user-agent');
  // test user-agent for 'Internet Explorer'
  if ((userAgent.match(/(MSIE)/i) || userAgent.match(/(Trident)/i)) && !userAgent.match(/(Edge)/i)) {
    return '/compiled'
  }
  return '/bundled';
}