index.js 936 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Module dependencies.
  3. */
  4. var resolve = require('path').resolve;
  5. var fs = require('co-fs');
  6. /**
  7. * Serve favicon.ico
  8. *
  9. * @param {String} path
  10. * @param {Object} [options]
  11. * @return {Function}
  12. * @api public
  13. */
  14. module.exports = function (path, options){
  15. var icon;
  16. if (path) path = resolve(path);
  17. options = options || {};
  18. var maxAge = options.maxAge == null
  19. ? 86400000
  20. : Math.min(Math.max(0, options.maxAge), 31556926000);
  21. return function *favicon(next){
  22. if ('/favicon.ico' != this.path) return yield next;
  23. if (!path) return;
  24. if ('GET' !== this.method && 'HEAD' !== this.method) {
  25. this.status = 'OPTIONS' == this.method ? 200 : 405;
  26. this.set('Allow', 'GET, HEAD, OPTIONS');
  27. return;
  28. }
  29. if (!icon) icon = yield fs.readFile(path);
  30. this.set('Cache-Control', 'public, max-age=' + (maxAge / 1000 | 0));
  31. this.type = 'image/x-icon';
  32. this.body = icon;
  33. };
  34. };