Web framework.
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.

index.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. var http = require("http");
  2. var urllib = require("url");
  3. var fs = require("fs");
  4. var res404 = "404 not found: {{method}} {{pathname}}";
  5. var res403 = "403 forbidden: {{method}} {{pathname}}";
  6. function template(tpml, args) {
  7. for (var i in args) {
  8. tpml = tpml.split("{{"+i+"}}").join(args[i]);
  9. }
  10. return tpml;
  11. }
  12. function resJson(obj) {
  13. this.writeHead(200, {
  14. "content-type": "application/json"
  15. });
  16. this.end(JSON.stringify(obj));
  17. }
  18. function methodsMatch(route, req) {
  19. if (route.method === "ALL")
  20. return true;
  21. if (route.method === "GET" && req.method === "HEAD")
  22. return true;
  23. return route.method === req.method;
  24. }
  25. class App {
  26. constructor(options) {
  27. options = options || {};
  28. // 403 and 404 response
  29. this.res404 = options.res404 || res404;
  30. this.res403 = options.res403 || res403;
  31. // Script to be served as /webframe.js
  32. this.clientScript = "";
  33. // Create server
  34. if (options.server !== undefined) {
  35. this.server = options.server;
  36. } else {
  37. this.server = http.createServer();
  38. var port = options.port || process.env.PORT | 8080;
  39. var host = options.host || "127.0.0.1";
  40. this.server.listen(port, host);
  41. this.info("Listening on "+host+":"+port);
  42. }
  43. this._routeMap = {};
  44. this._routes = [];
  45. this._transforms = {};
  46. // Add scripts
  47. if (options.client_utils) {
  48. this.addScript("client_utils",
  49. fs.readFileSync(__dirname+"/client/utils.js", "utf8"));
  50. }
  51. // Serve /webframe.js
  52. this.get("/webframe.js", (req, res) => {
  53. res.writeHead(200, {
  54. "Content-Type": "application/javascript"
  55. });
  56. res.end(this.clientScript);
  57. });
  58. // Listen for requests
  59. this.server.on("request", (req, res) => {
  60. res.json = resJson;
  61. var url = urllib.parse(req.url);
  62. req.urlobj = url;
  63. var route = null;
  64. // With HEAD requests, we don't want to write anything
  65. if (req.method === "HEAD") {
  66. res.write = function() {};
  67. var end = res.end;
  68. res.end = function() { end.call(res); }
  69. }
  70. // If the route is in the hash map, use that
  71. var r = this._routeMap[url.pathname];
  72. if (r && (methodsMatch(r, req))) {
  73. route = r;
  74. // Search through the routes list and look for matching routes
  75. } else {
  76. for (var i in this._routes) {
  77. var r = this._routes[i];
  78. if (!methodsMatch(r, req))
  79. continue;
  80. if (r.pattern.test(req.urlobj.pathname)) {
  81. route = r;
  82. break;
  83. }
  84. }
  85. }
  86. // If we still have no route, 404
  87. if (route === null) {
  88. res.writeHead(404);
  89. res.end(template(this.res404,
  90. { method: req.method, pathname: req.urlobj.pathname }));
  91. return;
  92. }
  93. // Run all the middleware stuff if applicable
  94. var self = this;
  95. if (route.middleware) {
  96. var cbs = route.middleware.length;
  97. function cb() {
  98. if (--cbs === 0)
  99. route.func(req, res, self);
  100. }
  101. for (var i in route.middleware) {
  102. route.middleware[i](req, res, cb);
  103. }
  104. // Just run the function if there's no middleware
  105. } else {
  106. route.func(req, res, this);
  107. }
  108. });
  109. }
  110. /*
  111. * Add route.
  112. * Args:
  113. * method: "GET", "POST", "PUT", "DELETE", "ALL"
  114. * path: path,
  115. * func: function(request, response)
  116. */
  117. route(method, path, middleware, func) {
  118. if (method !== "GET" && method !== "POST" && method !== "PUT" &&
  119. method !== "DELETE" && method !== "ALL") {
  120. throw new Error("Invalid method.");
  121. }
  122. // Before is optional
  123. if (func === undefined) {
  124. func = middleware;
  125. middleware = undefined;
  126. }
  127. // All necessary arguments must exist
  128. if (typeof path !== "string")
  129. throw new TypeError("Path must be a string.");
  130. if (typeof func !== "function")
  131. throw new TypeError("Func must be a function.");
  132. // All middlewares must exist
  133. if (middleware) {
  134. for (var i in middleware) {
  135. if (typeof middleware[i] !== "function") {
  136. this.panic(
  137. "Middleware "+i+" for "+method+" "+path+
  138. " is "+(typeof middleware[i])+", expected function.");
  139. return;
  140. }
  141. }
  142. }
  143. // Add to routes array or route map
  144. if (path[0] === "^") {
  145. var pat = new RegExp(path);
  146. this._routes.push({
  147. method: method,
  148. func: func,
  149. pattern: pat,
  150. middleware: middleware
  151. });
  152. } else {
  153. this._routeMap[path] = {
  154. method: method,
  155. func: func,
  156. middleware: middleware
  157. };
  158. }
  159. }
  160. /*
  161. * Add a transform.
  162. * Args:
  163. * ext: What extension to transform
  164. * mime: The mime-type the transformed code should be sent as
  165. * func: function(path, writeStream)
  166. *
  167. * writeStream: {
  168. * status: status code (should only be modified before write() or end())
  169. * headers: HTTP headers (should only be modified before write() or end())
  170. * write: function(data) - write data
  171. * end: function(data) - write data and end response
  172. * error: function(err) - end response and log error
  173. * }
  174. */
  175. transform(ext, mime, func) {
  176. if (typeof ext !== "string")
  177. throw new Error("Expected ext to be string, got "+(typeof ext)+".");
  178. if (typeof func !== "function")
  179. throw new Error("Expected func to be function, got "+(typeof func)+".");
  180. if (this._transforms[ext])
  181. throw new Error("Extension "+ext+" already has a transform.");
  182. this._transforms[ext] = {
  183. func: func,
  184. mime: mime
  185. };
  186. }
  187. /*
  188. * Add code to be served as /webframe.js
  189. */
  190. addScript(name, str) {
  191. var start = "(function "+name+"() {\n";
  192. var end = "\n})();\n"
  193. str = str.trim();
  194. this.clientScript += start + str + end;
  195. }
  196. /*
  197. * Template string
  198. */
  199. template(tmpl, args) { return template(tmpl, args); }
  200. /*
  201. * Utility methods for GET/POST/PUT/DELETE/ALL
  202. */
  203. get(path, middleware, func) { this.route("GET", path, middleware, func); }
  204. post(path, middleware, func) { this.route("POST", path, middleware, func); }
  205. put(path, middleware, func) { this.route("PUT", path, middleware, func); }
  206. delete(path, middleware, func) { this.route("DELETE", path, middleware, func); }
  207. all(path, middleware, func) { this.route("ALL", path, middleware, func); }
  208. /*
  209. * Logging
  210. */
  211. info(str) {
  212. console.log("Info: "+str);
  213. }
  214. notice(str) {
  215. console.log("Notice: "+str);
  216. }
  217. warning(str) {
  218. console.log("Warning: "+str);
  219. }
  220. panic(str) {
  221. console.log("PANIC: "+str);
  222. process.exit(1);
  223. }
  224. }
  225. exports.static = require("./js/static");
  226. exports.middleware = require("./js/middleware");;
  227. exports.App = App;