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.

loader.js 1.7KB

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