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.

220 lines
5.5 KiB

9 years ago
11 years ago
11 years ago
10 years ago
12 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
11 years ago
12 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
12 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
9 years ago
9 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
10 years ago
9 years ago
9 years ago
  1. [![view on npm](http://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
  2. [![npm module downloads](http://img.shields.io/npm/dt/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
  3. [![Build Status](https://travis-ci.org/75lb/local-web-server.svg?branch=master)](https://travis-ci.org/75lb/local-web-server)
  4. [![Dependency Status](https://david-dm.org/75lb/local-web-server.svg)](https://david-dm.org/75lb/local-web-server)
  5. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
  6. # local-web-server
  7. A simple web-server for productive front-end development.
  8. **Requires node v4.0.0 or higher**.
  9. ## Synopsis
  10. For the examples below, assume we're in a project directory looking like this:
  11. ```sh
  12. .
  13. ├── css
  14. │   └── style.css
  15. ├── index.html
  16. └── package.json
  17. ```
  18. ### Static site
  19. Fire up your static site on the default port:
  20. ```sh
  21. $ ws
  22. serving at http://localhost:8000
  23. ```
  24. ### Single Page Application
  25. You're building a web app with client-side routing, so mark `index.html` as the SPA.
  26. ```sh
  27. $ ws --spa index.html
  28. ```
  29. By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found` as there is no file at that location on disk. By marking `index.html` as the SPA you create this rule:
  30. *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 SPA.*
  31. ### Access Control
  32. Access to all files is allowed, beside those in the forbidden list (e.g. config files):
  33. ```sh
  34. $ ws --forbid .json .yml
  35. serving at http://localhost:8000
  36. ```
  37. ### URL rewriting
  38. When urls don't map to your directory structure, rewrite:
  39. ```sh
  40. $ ws --rewrite /css=>/build/css
  41. ```
  42. ### Proxy
  43. Rewrite to remote servers (proxy):
  44. ```sh
  45. $ ws --rewrite "/api => http://api.example.com/api" \
  46. "/npm => http://registry.npmjs.com" \
  47. "/user/:project/repo -> https://api.github.com/repos/:project"
  48. ```
  49. ### Stored config
  50. Always use this port and blacklist? Persist it to the config:
  51. ```json
  52. {
  53. "name": "example",
  54. "version": "1.0.0",
  55. "local-web-server": {
  56. "port": 8100,
  57. "forbid": "\\.json$"
  58. }
  59. }
  60. ```
  61. ### Logging
  62. By default, local-web-server outputs a simple, dynamic statistics view. To see traditional web server logs, use `--log-format`:
  63. ```sh
  64. $ ws --log-format combined
  65. serving at http://localhost:8000
  66. ::1 - - [16/Nov/2015:11:16:52 +0000] "GET / HTTP/1.1" 200 12290 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2562.0 Safari/537.36"
  67. ```
  68. ### Mock Responses
  69. *Coming soon*.
  70. ### Other features
  71. Compression, caching, simple statistics view, log, override mime types.
  72. ## Tips
  73. ### Use with Google DevTools Workspaces
  74. ### Log Visualisation
  75. Instructions for how to visualise log output using goaccess, logstalgia or gltail [here](https://github.com/75lb/local-web-server/wiki/Log-visualisation).
  76. ## Install
  77. Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
  78. ```sh
  79. $ npm install -g local-web-server
  80. ```
  81. This will install the `ws` tool globally. To see the available options, run:
  82. ```sh
  83. $ ws --help
  84. ```
  85. ## Distribute with your project
  86. The standard convention with client-server applications is to add an `npm start` command to launch the server component.
  87. 1\. Install the server as a dev dependency
  88. ```sh
  89. $ npm install local-web-server --save-dev
  90. ```
  91. 2\. Add a `start` command to your `package.json`:
  92. ```json
  93. {
  94. "name": "example",
  95. "version": "1.0.0",
  96. "local-web-server": {
  97. "port": 8100,
  98. "forbid": "\\.json$"
  99. },
  100. "scripts": {
  101. "start": "ws"
  102. }
  103. }
  104. ```
  105. 3\. Document how to build and launch your site
  106. ```sh
  107. $ npm install
  108. $ npm start
  109. serving at http://localhost:8100
  110. ```
  111. ## Storing default options
  112. To store per-project options, saving you the hassle of inputting them everytime, store them in the `local-web-server` property of your project's `package.json`:
  113. ```json
  114. {
  115. "name": "my-project",
  116. "version": "0.11.8",
  117. "local-web-server":{
  118. "port": 8100
  119. }
  120. }
  121. ```
  122. Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site):
  123. ```json
  124. {
  125. "port": 8100,
  126. "log-format": "tiny"
  127. }
  128. ```
  129. Or store global defaults in a `.local-web-server.json` file in your home directory.
  130. ```json
  131. {
  132. "port": 3000,
  133. "refresh-rate": 1000
  134. }
  135. ```
  136. All stored defaults are overriden by options supplied at the command line.
  137. To view your stored defaults, run:
  138. ```sh
  139. $ ws --config
  140. ```
  141. ## mime-types
  142. You can set additional mime-type/extension mappings, or override the defaults by setting a `mime` value in your local config. This value is passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine). Example:
  143. ```json
  144. {
  145. "mime": {
  146. "text/plain": [ "php", "pl" ]
  147. }
  148. }
  149. ```
  150. ## API Reference
  151. <a name="module_local-web-server"></a>
  152. ## local-web-server
  153. <a name="exp_module_local-web-server--localWebServer"></a>
  154. ### localWebServer([options]) ⏏
  155. Returns a Koa application
  156. **Kind**: Exported function
  157. | Param | Type | Description |
  158. | --- | --- | --- |
  159. | [options] | <code>object</code> | options |
  160. | [options.forbid] | <code>Array.&lt;regexp&gt;</code> | a list of forbidden routes. |
  161. **Example**
  162. ```js
  163. const localWebServer = require('local-web-server')
  164. localWebServer().listen(8000)
  165. ```
  166. ## Composition
  167. * * *
  168. &copy; 2015 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).