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.

283 lines
9.4 KiB

9 years ago
11 years ago
11 years ago
9 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
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
9 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
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
11 years ago
9 years ago
11 years ago
9 years ago
10 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. ***This is the documentation for the next version. For the previous release, see the `prev` branch. To install this prerelease: `$ npm i -g local-web-server@^1.0.0-beta`***
  7. # local-web-server
  8. A simple web-server for productive front-end development.
  9. **Requires node v4.0.0 or higher**.
  10. ## Synopsis
  11. For the examples below, we assume we're in a project directory looking like this:
  12. ```sh
  13. .
  14. ├── css
  15. │   └── style.css
  16. ├── index.html
  17. └── package.json
  18. ```
  19. All paths/routes are specified using [express syntax](http://expressjs.com/guide/routing.html#route-paths). To run the example projects linked below, clone the project, move into the example directory specified, run `ws`.
  20. ### Static site
  21. Fire up your static site on the default port:
  22. ```sh
  23. $ ws
  24. serving at http://localhost:8000
  25. ```
  26. [Example](https://github.com/75lb/local-web-server/tree/master/example/simple).
  27. ### Single Page Application
  28. You're building a web app with client-side routing, so mark `index.html` as the SPA.
  29. ```sh
  30. $ ws --spa index.html
  31. ```
  32. 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:
  33. *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.*
  34. [Example](https://github.com/75lb/local-web-server/tree/master/example/spa).
  35. ### Access Control
  36. By default, access to all files is allowed (including dot files). Use `--forbid` to establish a blacklist:
  37. ```sh
  38. $ ws --forbid '*.json' '*.yml'
  39. serving at http://localhost:8000
  40. ```
  41. [Example](https://github.com/75lb/local-web-server/tree/master/example/forbid).
  42. ### URL rewriting
  43. Your application requested `/css/style.css` but it's stored at `/build/css/style.css`. To avoid a 404 you need a rewrite rule:
  44. ```sh
  45. $ ws --rewrite '/css/style.css -> /build/css/style.css'
  46. ```
  47. Or, more generally (matching any stylesheet under `/css`):
  48. ```sh
  49. $ ws --rewrite '/css/:stylesheet -> /build/css/:stylesheet'
  50. ```
  51. With a deep CSS directory structure it may be easier to mount the entire contents of `/build/css` to the `/css` path:
  52. ```sh
  53. $ ws --rewrite '/css/* -> /build/css/$1'
  54. ```
  55. this rewrites `/css/a` as `/build/css/a`, `/css/a/b/c` as `/build/css/a/b/c` etc.
  56. #### Proxied requests
  57. If the `to` URL contains a remote host, local-web-server will act as a proxy - fetching and responding with the remote resource.
  58. Mount the npm registry locally:
  59. ```sh
  60. $ ws --rewrite '/npm/* -> http://registry.npmjs.org/$1'
  61. ```
  62. Map local requests for repo data to the Github API:
  63. ```sh
  64. $ ws --rewrite '/:user/repos/:name -> https://api.github.com/repos/:user/:name'
  65. ```
  66. [Example](https://github.com/75lb/local-web-server/tree/master/example/rewrite).
  67. ### Stored config
  68. Use the same port and blacklist every time? Persist it to `package.json`:
  69. ```json
  70. {
  71. "name": "example",
  72. "version": "1.0.0",
  73. "local-web-server": {
  74. "port": 8100,
  75. "forbid": "*.json"
  76. }
  77. }
  78. ```
  79. or `.local-web-server.json`
  80. ```json
  81. {
  82. "port": 8100,
  83. "forbid": "*.json"
  84. }
  85. ```
  86. local-web-server will merge and use all config found, searching from the current directory upward. In the case both `package.json` and `.local-web-server.json` config is found in the same directory, `.local-web-server.json` will take precedence. Command-line options take precedence over all.
  87. To inspect stored config, run:
  88. ```sh
  89. $ ws --config
  90. ```
  91. ### Logging
  92. By default, local-web-server outputs a simple, dynamic statistics view. To see traditional web server logs, use `--log-format`:
  93. ```sh
  94. $ ws --log-format combined
  95. serving at http://localhost:8000
  96. ::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"
  97. ```
  98. The format value supplied is passed directly to [morgan](https://github.com/expressjs/morgan). The exception is `--log-format none` which disables all output.
  99. ### Other usage
  100. #### Debugging
  101. Prints information about loaded middleware, arguments, remote proxy fetches etc.
  102. ```sh
  103. $ ws --verbose
  104. ```
  105. #### Compression
  106. Serve gzip-compressed resources, where applicable
  107. ```sh
  108. $ ws --compress
  109. ```
  110. #### Disable caching
  111. Disable etag response headers, forcing resources to be served in full every time.
  112. ```sh
  113. $ ws --no-cache
  114. ```
  115. #### mime-types
  116. You can set additional mime-type/extension mappings, or override the defaults by setting a `mime` value in the stored config. This value is passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine). Example:
  117. ```json
  118. {
  119. "mime": {
  120. "text/plain": [ "php", "pl" ]
  121. }
  122. }
  123. ```
  124. [Example](https://github.com/75lb/local-web-server/tree/master/example/mime-override).
  125. #### Log Visualisation
  126. Instructions for how to visualise log output using goaccess, logstalgia or gltail [here](https://github.com/75lb/local-web-server/blob/master/doc/visualisation.md).
  127. ## Install
  128. Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
  129. ```sh
  130. $ npm install -g local-web-server
  131. ```
  132. This will install the `ws` tool globally. To see the available options, run:
  133. ```sh
  134. $ ws --help
  135. ```
  136. ## Distribute with your project
  137. The standard convention with client-server applications is to add an `npm start` command to launch the server component.
  138. 1\. Install the server as a dev dependency
  139. ```sh
  140. $ npm install local-web-server --save-dev
  141. ```
  142. 2\. Add a `start` command to your `package.json`:
  143. ```json
  144. {
  145. "name": "example",
  146. "version": "1.0.0",
  147. "local-web-server": {
  148. "port": 8100,
  149. "forbid": "*.json"
  150. },
  151. "scripts": {
  152. "start": "ws"
  153. }
  154. }
  155. ```
  156. 3\. Document how to build and launch your site
  157. ```sh
  158. $ npm install
  159. $ npm start
  160. serving at http://localhost:8100
  161. ```
  162. ## API Reference
  163. * [local-web-server](#module_local-web-server)
  164. * [localWebServer([options])](#exp_module_local-web-server--localWebServer) ⇒ <code>[KoaApplication](https://github.com/koajs/koa/blob/master/docs/api/index.md#application)</code>
  165. * [~rewriteRule](#module_local-web-server--localWebServer..rewriteRule)
  166. <a name="exp_module_local-web-server--localWebServer"></a>
  167. ### localWebServer([options]) ⇒ <code>[KoaApplication](https://github.com/koajs/koa/blob/master/docs/api/index.md#application)</code> ⏏
  168. Returns a Koa application you can launch or mix into an existing app.
  169. **Kind**: Exported function
  170. **Params**
  171. - [options] <code>object</code> - options
  172. - [.static] <code>object</code> - koa-static config
  173. - [.root] <code>string</code> <code> = &quot;.&quot;</code> - root directory
  174. - [.options] <code>string</code> - [options](https://github.com/koajs/static#options)
  175. - [.serveIndex] <code>object</code> - koa-serve-index config
  176. - [.path] <code>string</code> <code> = &quot;.&quot;</code> - root directory
  177. - [.options] <code>string</code> - [options](https://github.com/expressjs/serve-index#options)
  178. - [.forbid] <code>Array.&lt;string&gt;</code> - A list of forbidden routes, each route being an [express route-path](http://expressjs.com/guide/routing.html#route-paths).
  179. - [.spa] <code>string</code> - specify an SPA file to catch requests for everything but static assets.
  180. - [.log] <code>object</code> - [morgan](https://github.com/expressjs/morgan) config
  181. - [.format] <code>string</code> - [log format](https://github.com/expressjs/morgan#predefined-formats)
  182. - [.options] <code>object</code> - [options](https://github.com/expressjs/morgan#options)
  183. - [.compress] <code>boolean</code> - Serve gzip-compressed resources, where applicable
  184. - [.mime] <code>object</code> - A list of mime-type overrides, passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine)
  185. - [.rewrite] <code>[Array.&lt;rewriteRule&gt;](#module_local-web-server--localWebServer..rewriteRule)</code> - One or more rewrite rules
  186. - [.verbose] <code>boolean</code> - Print detailed output, useful for debugging
  187. **Example**
  188. ```js
  189. const localWebServer = require('local-web-server')
  190. localWebServer().listen(8000)
  191. ```
  192. <a name="module_local-web-server--localWebServer..rewriteRule"></a>
  193. #### localWebServer~rewriteRule
  194. The `from` and `to` routes are specified using [express route-paths](http://expressjs.com/guide/routing.html#route-paths)
  195. **Kind**: inner typedef of <code>[localWebServer](#exp_module_local-web-server--localWebServer)</code>
  196. **Properties**
  197. | Name | Type | Description |
  198. | --- | --- | --- |
  199. | from | <code>string</code> | request route |
  200. | to | <code>string</code> | target route |
  201. **Example**
  202. ```json
  203. {
  204. "rewrite": [
  205. { "from": "/css/*", "to": "/build/styles/$1" },
  206. { "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
  207. { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
  208. ]
  209. }
  210. ```
  211. * * *
  212. &copy; 2015 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).