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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 || process.env.HOSTNAME || "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. * middleware: middleware array (optional)
  116. * func: function(request, response)
  117. */
  118. route(method, path, middleware, func) {
  119. if (method !== "GET" && method !== "POST" && method !== "PUT" &&
  120. method !== "DELETE" && method !== "ALL") {
  121. throw new Error("Invalid method.");
  122. }
  123. // Middleware is optional
  124. if (func === undefined) {
  125. func = middleware;
  126. middleware = undefined;
  127. }
  128. // All necessary arguments must exist
  129. if (typeof path !== "string")
  130. throw new TypeError("Path must be a string.");
  131. if (typeof func !== "function")
  132. throw new TypeError("Func must be a function.");
  133. // All middlewares must exist
  134. if (middleware) {
  135. for (var i in middleware) {
  136. if (typeof middleware[i] !== "function") {
  137. this.panic(
  138. "Middleware "+i+" for "+method+" "+path+
  139. " is "+(typeof middleware[i])+", expected function.");
  140. return;
  141. }
  142. }
  143. }
  144. // Add to routes array or route map
  145. if (path[0] === "^") {
  146. var pat = new RegExp(path);
  147. this._routes.push({
  148. method: method,
  149. func: func,
  150. pattern: pat,
  151. middleware: middleware
  152. });
  153. } else {
  154. this._routeMap[path] = {
  155. method: method,
  156. func: func,
  157. middleware: middleware
  158. };
  159. }
  160. }
  161. /*
  162. * Remove a route.
  163. * Args:
  164. * path: path
  165. */
  166. unroute(path) {
  167. if (path[0] === "^") {
  168. path = "/"+path+"/";
  169. for (var i in this._routes) {
  170. var str = this._routes[i].pattern.toString();
  171. if (path === str)
  172. delete this._routes[i];
  173. }
  174. } else {
  175. delete this._routeMap[path];
  176. }
  177. }
  178. /*
  179. * Add a transform.
  180. * Args:
  181. * ext: What extension to transform
  182. * mime: The mime-type the transformed code should be sent as
  183. * func: function(path, writeStream)
  184. *
  185. * writeStream: {
  186. * status: status code (should only be modified before write() or end())
  187. * headers: HTTP headers (should only be modified before write() or end())
  188. * write: function(data) - write data
  189. * end: function(data) - write data and end response
  190. * error: function(err) - end response and log error
  191. * }
  192. */
  193. transform(ext, mime, func) {
  194. if (typeof ext !== "string")
  195. throw new Error("Expected ext to be string, got "+(typeof ext)+".");
  196. if (typeof func !== "function")
  197. throw new Error("Expected func to be function, got "+(typeof func)+".");
  198. if (this._transforms[ext])
  199. throw new Error("Extension "+ext+" already has a transform.");
  200. this._transforms[ext] = {
  201. func: func,
  202. mime: mime
  203. };
  204. }
  205. /*
  206. * Add code to be served as /webframe.js
  207. */
  208. addScript(name, str) {
  209. var start = "(function "+name+"() {\n";
  210. var end = "\n})();\n"
  211. str = str.trim();
  212. this.clientScript += start + str + end;
  213. }
  214. /*
  215. * Template string
  216. */
  217. template(tmpl, args) { return template(tmpl, args); }
  218. /*
  219. * Utility methods for GET/POST/PUT/DELETE/ALL
  220. */
  221. get(path, middleware, func) { this.route("GET", path, middleware, func); }
  222. post(path, middleware, func) { this.route("POST", path, middleware, func); }
  223. put(path, middleware, func) { this.route("PUT", path, middleware, func); }
  224. delete(path, middleware, func) { this.route("DELETE", path, middleware, func); }
  225. all(path, middleware, func) { this.route("ALL", path, middleware, func); }
  226. /*
  227. * Logging
  228. */
  229. info(str) {
  230. console.log("Info: "+str);
  231. }
  232. notice(str) {
  233. console.log("Notice: "+str);
  234. }
  235. warning(str) {
  236. console.log("Warning: "+str);
  237. }
  238. panic(str) {
  239. console.log("PANIC: "+str);
  240. process.exit(1);
  241. }
  242. }
  243. exports.static = require("./js/static");
  244. exports.middleware = require("./js/middleware");;
  245. exports.App = App;