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

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