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.

242 lines
8.1 KiB

8 years ago
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
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
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. [![Coverage Status](https://coveralls.io/repos/github/lwsjs/local-web-server/badge.svg?branch=next)](https://coveralls.io/github/lwsjs/local-web-server?branch=next)
  5. [![Dependency Status](https://david-dm.org/lwsjs/local-web-server/next.svg)](https://david-dm.org/lwsjs/local-web-server/next)
  6. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
  7. [![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)
  8. **This documentation is a work in progress**
  9. # local-web-server
  10. The modular web server for productive full-stack development, powered by [lws](https://github.com/lwsjs/lws).
  11. Use this tool to:
  12. * Build any flavour of web application (static site, dynamic site with client or server-rendered content, Single Page App, Progessive Web App, Angular or React app etc.)
  13. * Prototype any CORS-enabled back-end service (e.g. RESTful HTTP API or Microservice using websockets, Server Sent Events etc.)
  14. * Monitor activity, analyse performance, experiment with caching strategies etc.
  15. * Build your own, personalised CLI web server tool
  16. Features:
  17. * Modular, extensible and easy to personalise. Create, share and consume only plugins which match your requirements.
  18. * Powerful, extensible command-line interface (add your own commands and options)
  19. * HTTP, HTTPS and experimental HTTP2 support
  20. * URL Rewriting to local or remote destinations
  21. * Single Page Application support
  22. * Response mocking
  23. * Configurable access log
  24. * Route blacklisting
  25. * HTTP Conditional Request support
  26. * Gzip response compression and much more
  27. ## Synopsis
  28. 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)).
  29. ### Static web site
  30. The most simple use case is to run `ws` without any arguments - this will **host the current directory as a static web site**. Navigating to the server will render a directory listing or your `index.html`, if that file exists.
  31. ```sh
  32. $ ws
  33. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  34. ```
  35. ### Single Page Application
  36. Serving a Single Page Application (an app with client-side routing, e.g. a React or Angular app) is as trivial as specifying the name of your single page:
  37. ```sh
  38. $ ws --spa index.html
  39. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  40. ```
  41. By default, requests for typical SPA paths (e.g. `/user/1`, `/login`) return `404 Not Found` as a file at that location does not exist. By marking `index.html` as the SPA you create this rule:
  42. *If a static file at the requested path exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then serve the specified SPA and handle the route client-side.*
  43. ### URL rewriting and proxied requests
  44. Another common use case is to **re-route certain requests to a remote server** if, for example, you'd like to use data from a different environment. The following command would proxy requests with a URL beginning with `http://127.0.0.1:8000/api/` to `https://internal-service.local/api/`:
  45. ```sh
  46. $ ws --rewrite '/api/* -> https://internal-service.local/api/$1'
  47. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  48. ```
  49. ### Mock responses
  50. 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, Mock Responses can fill the gap. Mocks are defined in a module which can be reused between projects.
  51. Trivial example - respond to a request for `/rivers` with some JSON. Save the following Javascript in a file named `example-mocks.js`.
  52. ```js
  53. module.exports = MockBase => class MockRivers extends MockBase {
  54. mocks () {
  55. return {
  56. route: '/rivers',
  57. responses: [
  58. {
  59. response: {
  60. type: 'json',
  61. body: [
  62. { name: 'Volga', drainsInto: 'Caspian Sea' },
  63. { name: 'Danube', drainsInto: 'Black Sea' },
  64. { name: 'Ural', drainsInto: 'Caspian Sea' },
  65. { name: 'Dnieper', drainsInto: 'Black Sea' }
  66. ]
  67. }
  68. }
  69. ]
  70. }
  71. }
  72. }
  73. ```
  74. Launch `ws` passing in your mocks module.
  75. ```sh
  76. $ ws --mocks example-mocks.js
  77. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  78. ```
  79. GET your rivers.
  80. ```sh
  81. $ curl http://127.0.0.1:8000/rivers
  82. [
  83. {
  84. "name": "Volga",
  85. "drainsInto": "Caspian Sea"
  86. },
  87. {
  88. "name": "Danube",
  89. "drainsInto": "Black Sea"
  90. },
  91. {
  92. "name": "Ural",
  93. "drainsInto": "Caspian Sea"
  94. },
  95. {
  96. "name": "Dnieper",
  97. "drainsInto": "Black Sea"
  98. }
  99. ]
  100. ```
  101. More detail can be added to mocks. This example, a RESTful `/users` API, adds responses handling `PUT`, `DELETE` and `POST`.
  102. ```js
  103. const users = [
  104. { id: 1, name: 'Lloyd', age: 40 },
  105. { id: 2, name: 'Mona', age: 34 },
  106. { id: 3, name: 'Francesco', age: 24 }
  107. ]
  108. module.exports = MockBase => class MockUsers extends MockBase {
  109. mocks () {
  110. /* response mocks for /users */
  111. return [
  112. {
  113. route: '/users',
  114. responses: [
  115. /* Respond with 400 Bad Request for PUT and DELETE requests (inappropriate on a collection) */
  116. { request: { method: 'PUT' }, response: { status: 400 } },
  117. { request: { method: 'DELETE' }, response: { status: 400 } },
  118. {
  119. /* for GET requests return the collection */
  120. request: { method: 'GET' },
  121. response: { type: 'json', body: users }
  122. },
  123. {
  124. /* for POST requests, create a new user and return its location */
  125. request: { method: 'POST' },
  126. response: function (ctx) {
  127. const newUser = ctx.request.body
  128. users.push(newUser)
  129. newUser.id = users.length
  130. ctx.status = 201
  131. ctx.response.set('Location', `/users/${newUser.id}`)
  132. }
  133. }
  134. ]
  135. }
  136. ]
  137. }
  138. }
  139. ```
  140. Launch `ws` passing in your mocks module:
  141. ```sh
  142. $ ws --mocks example-mocks.js
  143. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  144. ```
  145. Test your mock responses. A `POST` request should return a `201` with an empty body and the `Location` of the new resource.
  146. ```sh
  147. $ curl http://127.0.0.1:8000/users -H 'Content-type: application/json' -d '{ "name": "Anthony" }' -i
  148. HTTP/1.1 201 Created
  149. Vary: Origin
  150. Location: /users/4
  151. Content-Type: text/plain; charset=utf-8
  152. Content-Length: 7
  153. Date: Wed, 28 Jun 2017 20:31:19 GMT
  154. Connection: keep-alive
  155. Created
  156. ```
  157. A `GET` to `/users` should return our mock user data, including the record just added.
  158. ```sh
  159. $ curl http://127.0.0.1:8000/users
  160. [
  161. {
  162. "id": 1,
  163. "name": "Lloyd",
  164. "age": 40
  165. },
  166. {
  167. "id": 2,
  168. "name": "Mona",
  169. "age": 34
  170. },
  171. {
  172. "id": 3,
  173. "name": "Francesco",
  174. "age": 24
  175. },
  176. {
  177. "id": 4,
  178. "name": "Anthony"
  179. }
  180. ```
  181. ### HTTPS
  182. Launching a secure server is as simple as setting the `--https` flag. [See the wiki](https://github.com/lwsjs/local-web-server/wiki) for further configuration options and a guide on how to get the "green padlock" in your browser.
  183. ```sh
  184. $ ws --https
  185. Serving at https://mbp.local:8000, https://127.0.0.1:8000, https://192.168.0.100:8000
  186. ```
  187. ## Further Documentation
  188. [See the wiki for plenty more documentation and tutorials](https://github.com/lwsjs/local-web-server/wiki).
  189. ## Install
  190. 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.
  191. ```sh
  192. $ npm install -g local-web-server@next
  193. ```
  194. * * *
  195. &copy; 2013-17 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).