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.

246 lines
8.4 KiB

8 years ago
8 years ago
8 years ago
11 years ago
8 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
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.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=master)](https://travis-ci.org/lwsjs/local-web-server)
  4. [![Coverage Status](https://coveralls.io/repos/github/lwsjs/local-web-server/badge.svg?branch=master)](https://coveralls.io/github/lwsjs/local-web-server?branch=master)
  5. [![dependencies Status](https://david-dm.org/lwsjs/local-web-server/master/status.svg)](https://david-dm.org/lwsjs/local-web-server/master)
  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. **Requires node v7.6 or above. Upgraders, please read the [release notes](https://github.com/lwsjs/local-web-server/releases)**.
  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 is requested (e.g. `/css/style.css`) then serve it, if not (e.g. `/login`) then serve the specified SPA and handle the route client-side.*
  43. [Read more](https://github.com/lwsjs/local-web-server/wiki/How-to-serve-a-Single-Page-Application-(SPA)).
  44. ### URL rewriting and proxied requests
  45. 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/`:
  46. ```sh
  47. $ ws --rewrite '/api/* -> https://internal-service.local/api/$1'
  48. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  49. ```
  50. ### Mock responses
  51. 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.
  52. Trivial example - respond to a request for `/rivers` with some JSON. Save the following Javascript in a file named `example-mocks.js`.
  53. ```js
  54. module.exports = MockBase => class MockRivers extends MockBase {
  55. mocks () {
  56. return {
  57. route: '/rivers',
  58. responses: [
  59. {
  60. response: {
  61. type: 'json',
  62. body: [
  63. { name: 'Volga', drainsInto: 'Caspian Sea' },
  64. { name: 'Danube', drainsInto: 'Black Sea' },
  65. { name: 'Ural', drainsInto: 'Caspian Sea' },
  66. { name: 'Dnieper', drainsInto: 'Black Sea' }
  67. ]
  68. }
  69. }
  70. ]
  71. }
  72. }
  73. }
  74. ```
  75. Launch `ws` passing in your mocks module.
  76. ```sh
  77. $ ws --mocks example-mocks.js
  78. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  79. ```
  80. GET your rivers.
  81. ```sh
  82. $ curl http://127.0.0.1:8000/rivers
  83. [
  84. {
  85. "name": "Volga",
  86. "drainsInto": "Caspian Sea"
  87. },
  88. {
  89. "name": "Danube",
  90. "drainsInto": "Black Sea"
  91. },
  92. {
  93. "name": "Ural",
  94. "drainsInto": "Caspian Sea"
  95. },
  96. {
  97. "name": "Dnieper",
  98. "drainsInto": "Black Sea"
  99. }
  100. ]
  101. ```
  102. More detail can be added to mocks. This example, a RESTful `/users` API, adds responses handling `PUT`, `DELETE` and `POST`.
  103. ```js
  104. const users = [
  105. { id: 1, name: 'Lloyd', age: 40 },
  106. { id: 2, name: 'Mona', age: 34 },
  107. { id: 3, name: 'Francesco', age: 24 }
  108. ]
  109. module.exports = MockBase => class MockUsers extends MockBase {
  110. mocks () {
  111. /* response mocks for /users */
  112. return [
  113. {
  114. route: '/users',
  115. responses: [
  116. /* Respond with 400 Bad Request for PUT and DELETE requests (inappropriate on a collection) */
  117. { request: { method: 'PUT' }, response: { status: 400 } },
  118. { request: { method: 'DELETE' }, response: { status: 400 } },
  119. {
  120. /* for GET requests return the collection */
  121. request: { method: 'GET' },
  122. response: { type: 'json', body: users }
  123. },
  124. {
  125. /* for POST requests, create a new user and return its location */
  126. request: { method: 'POST' },
  127. response: function (ctx) {
  128. const newUser = ctx.request.body
  129. users.push(newUser)
  130. newUser.id = users.length
  131. ctx.status = 201
  132. ctx.response.set('Location', `/users/${newUser.id}`)
  133. }
  134. }
  135. ]
  136. }
  137. ]
  138. }
  139. }
  140. ```
  141. Launch `ws` passing in your mocks module:
  142. ```sh
  143. $ ws --mocks example-mocks.js
  144. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  145. ```
  146. Test your mock responses. A `POST` request should return a `201` with an empty body and the `Location` of the new resource.
  147. ```sh
  148. $ curl http://127.0.0.1:8000/users -H 'Content-type: application/json' -d '{ "name": "Anthony" }' -i
  149. HTTP/1.1 201 Created
  150. Vary: Origin
  151. Location: /users/4
  152. Content-Type: text/plain; charset=utf-8
  153. Content-Length: 7
  154. Date: Wed, 28 Jun 2017 20:31:19 GMT
  155. Connection: keep-alive
  156. Created
  157. ```
  158. A `GET` to `/users` should return our mock user data, including the record just added.
  159. ```sh
  160. $ curl http://127.0.0.1:8000/users
  161. [
  162. {
  163. "id": 1,
  164. "name": "Lloyd",
  165. "age": 40
  166. },
  167. {
  168. "id": 2,
  169. "name": "Mona",
  170. "age": 34
  171. },
  172. {
  173. "id": 3,
  174. "name": "Francesco",
  175. "age": 24
  176. },
  177. {
  178. "id": 4,
  179. "name": "Anthony"
  180. }
  181. ```
  182. See [the tutorials](https://github.com/lwsjs/local-web-server/wiki#tutorials) for more information and examples about mock responses.
  183. ### HTTPS
  184. 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.
  185. ```sh
  186. $ ws --https
  187. Serving at https://mbp.local:8000, https://127.0.0.1:8000, https://192.168.0.100:8000
  188. ```
  189. ## Further Documentation
  190. [See the wiki for plenty more documentation and tutorials](https://github.com/lwsjs/local-web-server/wiki).
  191. ## Install
  192. **Requires node v7.6 or above**. Install the [previous release](https://github.com/lwsjs/local-web-server/tree/v1.x) for node >= v4.0.0.
  193. ```sh
  194. $ npm install -g local-web-server
  195. ```
  196. * * *
  197. &copy; 2013-17 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).