popsicle-rewrite.js 479 B

123456789101112131415161718
  1. module.exports = rewritePopsicle
  2. function rewritePopsicle (rewrites) {
  3. var mappings = Object.keys(rewrites).map(function (pattern) {
  4. return [ new RegExp(pattern), rewrites[pattern] ]
  5. })
  6. return function (request, next) {
  7. for (var i = 0; i < mappings.length; i++) {
  8. var mapping = mappings[i]
  9. if (request.url.search(mapping[0]) >= 0) {
  10. request.url = request.url.replace(mapping[0], mapping[1])
  11. break
  12. }
  13. }
  14. return next()
  15. }
  16. }