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

add prefixing functionality #92

Closed
wants to merge 2 commits into from

Conversation

fooloomanzoo
Copy link

@fooloomanzoo fooloomanzoo commented Jun 28, 2017

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('/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)) {
    return '/compiled'
  }
  return '/bundled'
}

support for a prefix function in ptions to dynamically prefixing the
paths
@dougwilson
Copy link
Contributor

Why not just use Express directly? You can then alter any option based on any input, which is more flexible than the solution in this PR.

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

var app = express()

app.use(ifUserAgent(/(MSIE)/i, serveStatic('/compiled/build')))
app.use(ifNotUserAgent(/(MSIE)/i, serveStatic('/bundled/build')))

app.listen(3000)

function ifNotUserAgent (regex, middleware) {
  return function (req, res, next) {
    if (regex.test(req.get('user-agent'))) return next()
	return middleware(req, res, next)
  }
}

function ifUserAgent (regex, middleware) {
  return function (req, res, next) {
    if (!regex.test(req.get('user-agent'))) return next()
	return middleware(req, res, next)
  }
}

I'm sure you can simplify that wrapper even further and even publish as a Node.js module to allow folks to alter any middleware based on user agent.

@fooloomanzoo
Copy link
Author

You are totally right. I wasn't so forward thinking, in the express way. You could include this example in the README.md because it seams like a very good example for a middleware

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

Successfully merging this pull request may close these issues.

None yet

2 participants