Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

favicon.node.js 689B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var fs = require("fs");
  2. var zlib = require("zlib");
  3. var log = require("mlogger");
  4. var gzipped;
  5. var favicon;
  6. try {
  7. favicon = fs.readFileSync("favicon.ico");
  8. } catch (err) {
  9. if (err.code === "ENOENT")
  10. log.notice("No favicon.ico found.");
  11. else
  12. throw err;
  13. }
  14. if (favicon !== undefined) {
  15. zlib.gzip(favicon, function(err, res) {
  16. gzipped = res;
  17. });
  18. }
  19. module.exports = function(ctx) {
  20. if (favicon) {
  21. ctx.res.setHeader(
  22. "Cache-Control",
  23. "public, max-age="+ctx.conf.cache_max_age
  24. );
  25. }
  26. if (gzipped && ctx.shouldGzip) {
  27. ctx.res.setHeader("Content-Encoding", "gzip");
  28. ctx.res.end(gzipped);
  29. } else if (favicon) {
  30. ctx.res.end(favicon);
  31. } else {
  32. ctx.err404();
  33. }
  34. }