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.

142 lines
4.9 KiB

8 years ago
8 years ago
11 years ago
8 years ago
9 years ago
10 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
10 years ago
12 years ago
8 years ago
8 years ago
12 years ago
8 years ago
12 years ago
  1. [![npm (tag)](https://img.shields.io/npm/v/local-web-server/next.svg)](https://www.npmjs.org/package/local-web-server)
  2. [![npm module downloads](https://img.shields.io/npm/dt/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
  3. [![Build Status](https://travis-ci.org/lwsjs/local-web-server.svg?branch=next)](https://travis-ci.org/lwsjs/local-web-server)
  4. [![Dependency Status](https://david-dm.org/lwsjs/local-web-server/next.svg)](https://david-dm.org/lwsjs/local-web-server/next)
  5. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
  6. [![Join the chat at https://gitter.im/lwsjs/local-web-server](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/lwsjs/local-web-server?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
  7. **This documentation is a work in progress**
  8. # local-web-server
  9. The modular web server for productive full-stack development.
  10. Use this tool to:
  11. * Build fast, modern web applications using any tech, framework or architecture.
  12. * Prototype back-end services (RESTful HTTP API, Microservice, websocket server etc.)
  13. Features:
  14. * HTTP, HTTPS and HTTP2 support
  15. * Create, share and consume middleware, view and server modules
  16. * URL Rewriting, to local or remote destinations
  17. * Single Page Application support
  18. * Response mocking
  19. * Configurable access log
  20. * Route blacklisting
  21. * HTTP Conditional Request support (cacheing)
  22. * Gzip response compression and much more
  23. ## Synopsis
  24. This package installs the `ws` command-line tool (take a look at the [usage guide](https://github.com/lwsjs/local-web-server/wiki/CLI-usage)). The most simple use case is to run `ws` without any arguments - this will **host the current directory as a static web site**.
  25. ```sh
  26. $ ws
  27. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  28. ```
  29. Another common use case is to **proxy certain requests to remote servers** (e.g. you'd like to use data from a different environment). For example, the following command would proxy `http://127.0.0.1:8000/api/users/1` to `https://internal-service.local/api/users/1`:
  30. ```sh
  31. $ ws --rewrite '/api/* -> https://internal-service.local/api/$1`
  32. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  33. ```
  34. Imagine the network is down or you're working offline, proxied requests to `https://internal-service.local/api/users/1` would fail. In this case, you could use Mock Responses to fill the gap. Define the mock responses in a module.
  35. ```js
  36. const users = [
  37. { "id": 1, "name": "Lloyd", "age": 40, "nationality": "English" },
  38. { "id": 2, "name": "Mona", "age": 34, "nationality": "Palestinian" },
  39. { "id": 3, "name": "Francesco", "age": 24, "nationality": "Italian" }
  40. ]
  41. /* response mocks for /users */
  42. module.exports = [
  43. {
  44. route: '/users',
  45. responses: [
  46. /* Respond with 400 Bad Request for PUT and DELETE requests (inappropriate on a collection) */
  47. { request: { method: 'PUT' }, response: { status: 400 } },
  48. { request: { method: 'DELETE' }, response: { status: 400 } },
  49. {
  50. /* for GET requests return the collection */
  51. request: { method: 'GET' },
  52. response: { type: 'application/json', body: users }
  53. },
  54. {
  55. /* for POST requests, create a new user and return its location */
  56. request: { method: 'POST' },
  57. response: function (ctx) {
  58. const newUser = ctx.request.body
  59. users.push(newUser)
  60. ctx.status = 201
  61. ctx.response.set('Location', `/users/${users.length}`)
  62. }
  63. }
  64. ]
  65. }
  66. ]
  67. ```
  68. Next, launch `ws` passing in your mock response file:
  69. ```sh
  70. $ ws --mocks example-mocks.js
  71. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  72. ```
  73. Test your mock responses:
  74. ```sh
  75. $ curl http://127.0.0.1:8000/users -H 'Content-type: application/json' -d '{ "name": "Anthony" }' -i
  76. HTTP/1.1 201 Created
  77. Vary: Origin
  78. Location: /users/4
  79. Content-Type: text/plain; charset=utf-8
  80. Content-Length: 7
  81. Date: Wed, 28 Jun 2017 20:31:19 GMT
  82. Connection: keep-alive
  83. $ curl http://127.0.0.1:8000/users
  84. [
  85. {
  86. "id": 1,
  87. "name": "Lloyd",
  88. "age": 40,
  89. "nationality": "English"
  90. },
  91. {
  92. "id": 2,
  93. "name": "Mona",
  94. "age": 34,
  95. "nationality": "Palestinian"
  96. },
  97. {
  98. "id": 3,
  99. "name": "Francesco",
  100. "age": 24,
  101. "nationality": "Italian"
  102. },
  103. {
  104. "id": 4,
  105. "name": "Anthony"
  106. }
  107. ```
  108. ## Advanced Usage
  109. Being modular and extensible, features can be added or removed from `ws` in the shape of Middleware, ServerFactory or View modules. [See the wiki for full documentation and tutorials](https://github.com/lwsjs/local-web-server/wiki).
  110. ## Install
  111. Requires node v7.6 or higher. Install the [previous release](https://github.com/lwsjs/local-web-server/tree/v1.x) for node >= v4.0.0.
  112. ```sh
  113. $ npm install -g local-web-server@next
  114. ```
  115. * * *
  116. &copy; 2013-17 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).