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.

218 lines
5.5 KiB

9 years ago
10 years ago
10 years ago
9 years ago
11 years ago
10 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
9 years ago
10 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
11 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 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 a file does not exist with that path. 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 for client-side processing.*
  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. Rewrite to remote servers (proxy):
  43. ```sh
  44. $ ws --rewrite "/api => http://api.example.com/api" \
  45. "/npm => http://registry.npmjs.com" \
  46. "/user/:project/repo -> https://api.github.com/repos/:project"
  47. ```
  48. ### Stored config
  49. Always use this port and blacklist? Persist it to the config:
  50. ```json
  51. {
  52. "name": "example",
  53. "version": "1.0.0",
  54. "local-web-server": {
  55. "port": 8100,
  56. "forbid": "\\.json$"
  57. }
  58. }
  59. ```
  60. ### Logging
  61. By default, local-web-server outputs a simple, dynamic statistics view. To see traditional web server logs, use `--log-format`:
  62. ```sh
  63. $ ws --log-format combined
  64. serving at http://localhost:8000
  65. ::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"
  66. ```
  67. ### Mock Responses
  68. *Coming soon*.
  69. ### Other features
  70. Compression, caching, simple statistics view, log, override mime types.
  71. ## Tips
  72. ### Use with Google DevTools Workspaces
  73. ### Log Visualisation
  74. Instructions for how to visualise log output using goaccess, logstalgia or gltail [here](https://github.com/75lb/local-web-server/wiki/Log-visualisation).
  75. ## Install
  76. Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
  77. ```sh
  78. $ npm install -g local-web-server
  79. ```
  80. This will install the `ws` tool globally. To see the available options, run:
  81. ```sh
  82. $ ws --help
  83. ```
  84. ## Distribute with your project
  85. The standard convention with client-server applications is to add an `npm start` command to launch the server component.
  86. 1\. Install the server as a dev dependency
  87. ```sh
  88. $ npm install local-web-server --save-dev
  89. ```
  90. 2\. Add a `start` command to your `package.json`:
  91. ```json
  92. {
  93. "name": "example",
  94. "version": "1.0.0",
  95. "local-web-server": {
  96. "port": 8100,
  97. "forbid": "\\.json$"
  98. },
  99. "scripts": {
  100. "start": "ws"
  101. }
  102. }
  103. ```
  104. 3\. Document how to build and launch your site
  105. ```sh
  106. $ npm install
  107. $ npm start
  108. serving at http://localhost:8100
  109. ```
  110. ## Storing default options
  111. 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`:
  112. ```json
  113. {
  114. "name": "my-project",
  115. "version": "0.11.8",
  116. "local-web-server":{
  117. "port": 8100
  118. }
  119. }
  120. ```
  121. Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site):
  122. ```json
  123. {
  124. "port": 8100,
  125. "log-format": "tiny"
  126. }
  127. ```
  128. Or store global defaults in a `.local-web-server.json` file in your home directory.
  129. ```json
  130. {
  131. "port": 3000,
  132. "refresh-rate": 1000
  133. }
  134. ```
  135. All stored defaults are overriden by options supplied at the command line.
  136. To view your stored defaults, run:
  137. ```sh
  138. $ ws --config
  139. ```
  140. ## mime-types
  141. 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:
  142. ```json
  143. {
  144. "mime": {
  145. "text/plain": [ "php", "pl" ]
  146. }
  147. }
  148. ```
  149. ## API Reference
  150. <a name="module_local-web-server"></a>
  151. ## local-web-server
  152. <a name="exp_module_local-web-server--localWebServer"></a>
  153. ### localWebServer([options]) ⏏
  154. Returns a Koa application
  155. **Kind**: Exported function
  156. | Param | Type | Description |
  157. | --- | --- | --- |
  158. | [options] | <code>object</code> | options |
  159. | [options.forbid] | <code>Array.&lt;regexp&gt;</code> | a list of forbidden routes. |
  160. **Example**
  161. ```js
  162. const localWebServer = require('local-web-server')
  163. localWebServer().listen(8000)
  164. ```
  165. ## Composition
  166. * * *
  167. &copy; 2015 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).