Simple image host.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

loader.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var fs = require("fs");
  2. var zlib = require("zlib");
  3. var minify = require("./minify.js");
  4. var includeHtml = require("./includeHtml.js");
  5. exports.load = function(endpoints, conf) {
  6. var res = {
  7. endpoints: {},
  8. templates: {},
  9. views: {}
  10. }
  11. //Prepare endpoints
  12. var errs = false;
  13. Object.keys(endpoints).forEach(function(i) {
  14. var ep = endpoints[i];
  15. try {
  16. //The endpoint is a function if the file ends with .node.js
  17. if (/\.node\.js$/.test(ep)) {
  18. res.endpoints[i] = require("../"+conf.webroot+"/"+ep);
  19. //If it doesn't end with .node.js, it's a regular text file and will
  20. //just be served as is
  21. } else {
  22. var str = fs.readFileSync(conf.webroot+"/"+ep, "utf8");
  23. //If it's an HTML file, we minify it
  24. if (!conf.minify) {
  25. //Don't minify unless the conf tells us to
  26. } else if (/\.html$/.test(ep)) {
  27. str = minify.html(str);
  28. } else if (/\.js$/.test(ep)) {
  29. str = minify.js(str);
  30. } else if (/\.css$/.test(ep)) {
  31. str = minify.css(str);
  32. }
  33. res.endpoints[i] = str;
  34. }
  35. //Errors will usually be because an endpoint doesn't exist
  36. } catch (err) {
  37. if (err.code == "ENOENT") {
  38. console.log(err.toString());
  39. errs = true;
  40. } else {
  41. throw err;
  42. }
  43. }
  44. });
  45. //No need to proceed if some endpoints don't exist
  46. if (errs) process.exit();
  47. //Prepare all templates
  48. var templates = {};
  49. fs.readdirSync("templates").forEach(function(f) {
  50. var name = f.replace(/\.html$/, "");
  51. res.templates[name] = includeHtml("templates/"+f, conf);
  52. });
  53. //Prepare all views
  54. var views = {};
  55. fs.readdirSync("views").forEach(function(f) {
  56. var name = f.replace(/\.html$/, "");
  57. res.views[name] = includeHtml("views/"+f, conf);
  58. });
  59. return res;
  60. }