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.

180 lines
6.7 KiB

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