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.

181 lines
6.7 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
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.
  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. Features:
  16. * Modular, extensible and easy to personalise. Create, share and consume the plugins which match your requirements.
  17. * Powerful, extensible command-line interface (add your own commands and options)
  18. * HTTP, HTTPS and experimental HTTP2 support
  19. * URL Rewriting to local or remote destinations
  20. * Single Page Application support
  21. * Response mocking
  22. * Configurable access log
  23. * Route blacklisting
  24. * HTTP Conditional Request support
  25. * Gzip response compression and much more
  26. ## Synopsis
  27. 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)).
  28. ### Static web site
  29. 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.
  30. ```sh
  31. $ ws
  32. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  33. ```
  34. ### Single Page Application
  35. Serving a Single Page Application (e.g. a React or Angular app) is as trivial as specifying the name of your single page:
  36. ```sh
  37. $ ws --spa index.html
  38. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  39. ```
  40. By default, requests for typical SPA paths (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file at that locaiton does not exist. By marking `index.html` as the SPA you create this rule:
  41. *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.*
  42. ### URL rewriting and proxied requests
  43. 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/`:
  44. ```sh
  45. $ ws --rewrite '/api/* -> https://internal-service.local/api/$1'
  46. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  47. ```
  48. ### Mock responses
  49. 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. Export your mock responses from a module.
  50. ```js
  51. const users = [
  52. { "id": 1, "name": "Lloyd", "age": 40 },
  53. { "id": 2, "name": "Mona", "age": 34 },
  54. { "id": 3, "name": "Francesco", "age": 24 }
  55. ]
  56. module.exports = MockBase => class MockUsers extends MockBase {
  57. mocks () {
  58. /* response mocks for /users */
  59. return [
  60. {
  61. route: '/users',
  62. responses: [
  63. /* Respond with 400 Bad Request for PUT and DELETE requests (inappropriate on a collection) */
  64. { request: { method: 'PUT' }, response: { status: 400 } },
  65. { request: { method: 'DELETE' }, response: { status: 400 } },
  66. {
  67. /* for GET requests return the collection */
  68. request: { method: 'GET' },
  69. response: { type: 'application/json', body: users }
  70. },
  71. {
  72. /* for POST requests, create a new user and return its location */
  73. request: { method: 'POST' },
  74. response: function (ctx) {
  75. const newUser = ctx.request.body
  76. users.push(newUser)
  77. newUser.id = users.length
  78. ctx.status = 201
  79. ctx.response.set('Location', `/users/${newUser.id}`)
  80. }
  81. }
  82. ]
  83. }
  84. ]
  85. }
  86. }
  87. ```
  88. Next, launch `ws` passing in your mocks module:
  89. ```sh
  90. $ ws --mocks example-mocks.js
  91. Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
  92. ```
  93. Test your mock responses. A `POST` request should return a `201` with a `Location` header and empty body.
  94. ```sh
  95. $ curl http://127.0.0.1:8000/users -H 'Content-type: application/json' -d '{ "name": "Anthony" }' -i
  96. HTTP/1.1 201 Created
  97. Vary: Origin
  98. Location: /users/4
  99. Content-Type: text/plain; charset=utf-8
  100. Content-Length: 7
  101. Date: Wed, 28 Jun 2017 20:31:19 GMT
  102. Connection: keep-alive
  103. ```
  104. A `GET` to `/users` should return our mock user data, including the record just added.
  105. ```sh
  106. $ curl http://127.0.0.1:8000/users
  107. [
  108. {
  109. "id": 1,
  110. "name": "Lloyd",
  111. "age": 40
  112. },
  113. {
  114. "id": 2,
  115. "name": "Mona",
  116. "age": 34
  117. },
  118. {
  119. "id": 3,
  120. "name": "Francesco",
  121. "age": 24
  122. },
  123. {
  124. "id": 4,
  125. "name": "Anthony"
  126. }
  127. ```
  128. ### HTTPS
  129. 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.
  130. ```sh
  131. $ ws --https
  132. Serving at https://mbp.local:8000, https://127.0.0.1:8000, https://192.168.0.100:8000
  133. ```
  134. ## Further Documentation
  135. [See the wiki for plenty more documentation and tutorials](https://github.com/lwsjs/local-web-server/wiki).
  136. ## Install
  137. 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.
  138. ```sh
  139. $ npm install -g local-web-server@next
  140. ```
  141. * * *
  142. &copy; 2013-17 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).