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.

166 lines
6.6 KiB

7 years ago
7 years ago
11 years ago
7 years ago
10 years ago
9 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
9 years ago
10 years ago
11 years ago
7 years ago
11 years ago
7 years ago
11 years ago
  1. [![view on npm](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. [![Dependency Status](https://badgen.net/david/dep/lwsjs/local-web-server)](https://david-dm.org/lwsjs/local-web-server)
  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 v8 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.
  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 HTTP2 support (HTTP2 requires node v8.4.0 or above)
  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 and Range request support
  26. * Gzip response compression, HTTP Basic Authentication 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. ### HTTPS
  51. 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.
  52. ```sh
  53. $ ws --https
  54. Serving at https://mbp.local:8000, https://127.0.0.1:8000, https://192.168.0.100:8000
  55. ```
  56. ### HTTP2
  57. Uses node's built-in HTTP2 support. HTTP2 servers are always secure using local-web-server's built-in SSL certificates (by default) or those supplied by `--cert`, `--key` or `--pfx`. [See the wiki](https://github.com/lwsjs/local-web-server/wiki) for further info about HTTPS options and a guide on how to get the "green padlock" in your browser.
  58. ```sh
  59. $ ws --http2
  60. Serving at https://mbp.local:8000, https://127.0.0.1:8000, https://192.168.0.100:8000
  61. ```
  62. ### Mock responses
  63. 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.
  64. Trivial example - respond to a request for `/rivers` with some JSON. Save the following Javascript in a file named `example-mocks.js`.
  65. ```js
  66. module.exports = MockBase => class MockRivers extends MockBase {
  67. mocks () {
  68. return {
  69. route: '/rivers',
  70. responses: [
  71. {
  72. response: {
  73. type: 'json',
  74. body: [
  75. { name: 'Volga', drainsInto: 'Caspian Sea' },
  76. { name: 'Danube', drainsInto: 'Black Sea' },
  77. { name: 'Ural', drainsInto: 'Caspian Sea' },
  78. { name: 'Dnieper', drainsInto: 'Black Sea' }
  79. ]
  80. }
  81. }
  82. ]
  83. }
  84. }
  85. }
  86. ```
  87. 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. GET your rivers.
  93. ```sh
  94. $ curl http://127.0.0.1:8000/rivers
  95. [
  96. {
  97. "name": "Volga",
  98. "drainsInto": "Caspian Sea"
  99. },
  100. {
  101. "name": "Danube",
  102. "drainsInto": "Black Sea"
  103. },
  104. {
  105. "name": "Ural",
  106. "drainsInto": "Caspian Sea"
  107. },
  108. {
  109. "name": "Dnieper",
  110. "drainsInto": "Black Sea"
  111. }
  112. ]
  113. ```
  114. See [the tutorials](https://github.com/lwsjs/local-web-server/wiki#tutorials) for more information and examples about mock responses.
  115. ## Further Documentation
  116. [See the wiki for plenty more documentation and tutorials](https://github.com/lwsjs/local-web-server/wiki).
  117. ## Install
  118. **Requires node v8 or above**. Install the [previous release](https://github.com/lwsjs/local-web-server/tree/v1.x) for node >= v4.0.0.
  119. ```sh
  120. $ npm install -g local-web-server
  121. ```
  122. * * *
  123. &copy; 2013-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).