Simple image host.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

loader.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 (/\.html$/.test(ep)) {
  24. res.endpoints[i] = minify.html(res.endpoints[i]);
  25. } else if (/\.js$/.test(ep)) {
  26. res.endpoints[i] = minify.js(res.endpoints[i]);
  27. } else if (/\.css$/.test(ep)) {
  28. res.endpoints[i] = minify.css(res.endpoints[i]);
  29. }
  30. }
  31. //Errors will usually be because an endpoint doesn't exist
  32. } catch (err) {
  33. if (err.code == "ENOENT") {
  34. console.log(err.toString());
  35. errs = true;
  36. } else {
  37. throw err;
  38. }
  39. }
  40. });
  41. //No need to proceed if some endpoints don't exist
  42. if (errs) process.exit();
  43. //Prepare all templates
  44. var templates = {};
  45. fs.readdirSync("templates").forEach(function(f) {
  46. var name = f.replace(/\.html$/, "");
  47. res.templates[name] = includeHtml("templates/"+f, conf.web);
  48. });
  49. //Prepare all views
  50. var views = {};
  51. fs.readdirSync("views").forEach(function(f) {
  52. var name = f.replace(/\.html$/, "");
  53. res.views[name] = includeHtml("views/"+f, conf.web);
  54. });
  55. return res;
  56. }