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.

README.md 5.5KB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # webframe
  2. Webframe is a small and dependency free web application framework.
  3. ## Usage
  4. Install:
  5. ``` bash
  6. npm install --save webframe
  7. ```
  8. Use:
  9. ``` JavaScript
  10. var webframe = require("webframe");
  11. var app = new webframe.App();
  12. ```
  13. Or from the command line:
  14. ``` Bash
  15. npm install -g webframe
  16. webframe [directory]
  17. ```
  18. The file example.js contains an example of a small web application.
  19. ## Why a new web framework?
  20. A new installation of express is 2.3 megabytes. You end up with 42 dependencies
  21. in your node\_modules directory. The Koa framework is 2.5 megabytes, with 36
  22. dependencies. This isn't bad for a big web application, but I write a lot of
  23. tiny web applications where megabytes of dependencies just feels wrong. As a
  24. result, I have in the past often just used the http module directly. This
  25. framework is a nice middle ground; it provides some of the utility of big web
  26. frameworks, but without adding a ton of extra dependencies to the application.
  27. ## Documentation
  28. ### webframe.App([options])
  29. Creates a new instance, with an optional options argument.
  30. Options:
  31. * `server`: Your own HTTP (like the one created by http.createHttpServer).
  32. * `listen`: Whether to call the `listen` method on the server. Defaults to
  33. true.
  34. * `port`: The port to listen on. Defaults to the PORT environment variable, or
  35. 8080.
  36. * `host`: The host to listen on. Defaults to the HOSTNAME environment variable,
  37. or "127.0.0.1". For the server to be accessible to the outside world, listen
  38. on "0.0.0.0".
  39. * `client_utils`: Whether to add some utility functions to /webframe.js.
  40. Defaults to false.
  41. * `res404`: The string to return for a 404 error. "{{pathname}}" will be
  42. replaced with the pathname.
  43. * `res403`: The string to return for a 403 error. "{{pathname}}" will be
  44. replaced with the pathname.
  45. ``` JavaScript
  46. var app = new webframe.App({ client_utils: true });
  47. ```
  48. ### app.route(method, path [, middleware], func)
  49. Add a route.
  50. * `method`: What HTTP method the route should be available at.
  51. Valid options: "GET", "POST", "PUT", "DELETE", or "ALL".
  52. * `path`: The path. If it starts with a "^", it will be interpreted as a
  53. regular expression (with `new RegExp(path)`), and the route will be ran on
  54. all matching paths. Routes without a pattern are prioritized over those with
  55. a pattern.
  56. * `middleware`: Optional. An array of middleware functions which will be ran
  57. before the route's function.
  58. * `func`: function(request, response). The function which handles the request.
  59. ### app.get, app.post, app.put, app.delete, app.all
  60. Utility functions which just call app.route() with a predefined method value.
  61. E.g `app.get("/foo", handler)` is the same as
  62. `app.route("GET", "/foo", handler)`.
  63. ### app.info(str), app.notice(str), app.warning(str), app.panic(str)
  64. Logging functions. They all print a message. `app.panic` also exits, and is
  65. intended for errors so bad you have to crash.
  66. In the future, it will be possible to control the logging level.
  67. ### Middleware
  68. `webframe.middleware` contains middleware for the route functions.
  69. * `webframe.middleware.cookies`: Adds `request.cookies`, parsed COOKIE header.
  70. * `webframe.middleware.params`: Adds `request.params`, parsed URL parameters.
  71. * `webframe.middleware.payload`: Adds `request.payload`, a string (max 50
  72. kilobytes) of the request payload.
  73. In the future, there will be middleware for form data over POST too.
  74. ### Modifications to the request and response objects
  75. * `req.urlobj`: The result of `url.parse(req.url)`.
  76. * `res.json(object)`: A utility function to respond with JSON.
  77. ### webframe.static(root[, before])
  78. Serve static files.
  79. * `app.get("^.*", webframe.static("web"))` will serve all the files in the
  80. `web` directory, so `/index.html` will serve `web/index.html`.
  81. * `app.get("^/static/.*", webframe.static("web", "/static"))` will serve all
  82. files in the `web` directory under `/static`, so `/static/script.js` will
  83. serve `web/script.js`.
  84. ### Transformations - app.transform(extension, mime, func)
  85. Webframe has a way to transform files of one file type to an other, for example
  86. transforming typescript into javascript, or sass into CSS.
  87. * `extension`: The file extension to transform.
  88. * `mime`: The result mime type.
  89. * `func`: function(path, writeStream). The transform function.
  90. * `writeStream.headersSent`: Whether headers have been sent or not.
  91. * `writeStream.status`: The status code. Default: 200 (or 500 for errors)
  92. * `writeStream.headers`: An object containing the headers to be sent.
  93. Default: `{ "content-type": <mime> }`
  94. * `writeStream.write`: function(data). Write response data.
  95. * `writeStream.end`: function([data]). End the request, optionally with
  96. response data.
  97. * `writeStream.error`: function(err). Log an error, respond with status
  98. code 500 (or the content of `writeStream.status` if it's not 200).
  99. Example (assumes you have the node-sass module installed):
  100. ```
  101. var sass = require("node-sass");
  102. app.transform(".sass", "text/css", (path, writeStream) => {
  103. try {
  104. var str = fs.readFileSync(path, "utf8");
  105. } catch (err) {
  106. writeStream.status = 404;
  107. return writeStream.error(err);
  108. }
  109. try {
  110. var result = sass.renderSync({ data: str });
  111. writeStream.end(result.css);
  112. } catch (err) {
  113. writeStream.error(err);
  114. }
  115. });
  116. ```
  117. Now, any .sass file served with `webframe.static` will be compiled into CSS,
  118. and sent with the browser with the MIME type "text/css". The result is also
  119. cached, so the expensive sass compilation will only happen the first time the
  120. file is requested, or when the file changes. The result will not be cached
  121. if you call writeStream.error.