http and express middleware serves selective polyfills for javascript based on the requesting user-agent.
See the polyfill project for details about which polyfills and browsers are supported.
npm install polyfill-middleware
You can use the polyfill middleware with express by specifying which path to serve the dynamically generated js from.
The below examples assumes the following script tag <script src="/assets/polyfill.js"></script>
var polyfill = require('polyfill-middleware');
app.use('/assets/polyfill.js', polyfill());
// alternatively you can limit to just `get` requests
app.get('/assets/polyfill.js', polyfill());
The middleware function takes 3 arguments (request, response, done). done
should be a function and will be called if the polyfill middleware encountered an error. If there was no error, polyfill middleware will respond to the request.
var middleware = polyfill();
http.createServer(function(req, res) {
// .. some logic to determine if the request should be handled by polyfill
middleware(req, res, function(err) {
});
});
More examples are available in the examples
directory.