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 2.0KB

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