Web framework.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 7 Jahren
vor 7 Jahren
vor 7 Jahren
vor 7 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. The file example.js contains an example of a small web application.
  14. ## Why a new web framework?
  15. A new installation of express is 2.3 megabytes. You end up with 42 dependencies
  16. in your node\_modules directory. The Koa framework is 2.5 megabytes, with 36
  17. dependencies. This isn't bad for a big web application, but I write a lot of
  18. tiny web applications where megabytes of dependencies just feels wrong. As a
  19. result, I have in the past often just used the http module directly. This
  20. framework is a nice middle ground; it provides some of the utility of big web
  21. frameworks, but without adding a ton of extra dependencies to the application.
  22. ## Documentation
  23. ### webframe.App([options])
  24. Creates a new instance, with an optional options argument.
  25. Options:
  26. * `server`: Your own HTTP (like the one created by http.createHttpServer).
  27. * `port`: The port to listen on. Defaults to the PORT environment variable, or
  28. 8080.
  29. * `host`: The host to listen on. Defaults to "127.0.0.1". For the server to be
  30. accessible to the outside world, listen on "0.0.0.0".
  31. * `client_utils`: Whether to add some utility functions to /webframe.js.
  32. Defaults to false.
  33. * `res404`: The string to return for a 404 error. "{{pathname}}" will be
  34. replaced with the pathname.
  35. * `res403`: The string to return for a 403 error. "{{pathname}}" will be
  36. replaced with the pathname.
  37. ``` JavaScript
  38. var app = new webframe.App({ client_utils: true });
  39. ```
  40. ### app.route(method, path [, middleware], func)
  41. Add a route.
  42. * `method`: What HTTP method the route should be available at.
  43. Valid options: "GET", "POST", "PUT", "DELETE", or "ALL".
  44. * `path`: The path. If it starts with a "^", it will be interpreted as a
  45. regular expression (with `new RegExp(path)`), and the route will be ran on
  46. all matching paths. Routes without a pattern are prioritized over those with
  47. a pattern.
  48. * `middleware`: Optional. An array of middleware functions which will be ran
  49. before the route's function.
  50. * `func`: function(request, response). The function which handles the request.
  51. ### app.get, app.post, app.put, app.delete, app.all
  52. Utility functions which just call app.route() with a predefined method value.
  53. E.g `app.get("/foo", handler)` is the same as
  54. `app.route("GET", "/foo", handler)`.
  55. ### app.info(str), app.notice(str), app.warning(str), app.panic(str)
  56. Logging functions. They all print a message. `app.panic` also exits, and is
  57. intended for errors so bad you have to crash.
  58. In the future, it will be possible to control the logging level.
  59. ### Middleware
  60. `webframe.middleware` contains middleware for the route functions.
  61. * `webframe.middleware.cookies`: Adds `request.cookies`, parsed COOKIE header.
  62. * `webframe.middleware.params`: Adds `request.params`, parsed URL parameters.
  63. * `webframe.middleware.payload`: Adds `request.payload`, a string (max 50
  64. kilobytes) of the request payload.
  65. In the future, there will be middleware for form data over POST too.
  66. ### Modifications to the request and response objects
  67. * `req.urlobj`: The result of `url.parse(req.url)`.
  68. * `res.json(object)`: A utility function to respond with JSON.
  69. ### webframe.static(root[, before])
  70. Serve static files.
  71. * `app.get("^.*", webframe.static("web"))` will serve all the files in the
  72. `web` directory, so `/index.html` will serve `web/index.html`.
  73. * `app.get("^/static/.*", webframe.static("web", "/static"))` will serve all
  74. files in the `web` directory under `/static`, so `/static/script.js` will
  75. serve `web/script.js`.