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.

332 lines
8.6 KiB

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
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
  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. Typical use cases:
  9. * Front-end Development
  10. * Static or Single Page App development
  11. * reroute paths to local or remote resources
  12. * Bundle with your front-end project
  13. * Very little configuration, just a few options
  14. * Outputs a dynamic statistics view to the terminal
  15. * Configurable log output, compatible with [Goaccess, Logstalgia and glTail](https://github.com/75lb/local-web-server/blob/master/doc/visualisation.md)
  16. * Back-end service mocking
  17. * Prototype a web service, microservice, REST API etc.
  18. * CORS-friendly, all origins allowed by default.
  19. * Proxy server
  20. * Useful to workaround CORS issues with remote servers
  21. * File sharing
  22. **Requires node v4.0.0 or higher**.
  23. ## Synopsis
  24. For the examples below, we assume we're in a project directory looking like this:
  25. ```sh
  26. .
  27. ├── css
  28. │   └── style.css
  29. ├── index.html
  30. └── package.json
  31. ```
  32. 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`.
  33. ### Static site
  34. Fire up your static site on the default port:
  35. ```sh
  36. $ ws
  37. serving at http://localhost:8000
  38. ```
  39. [Example](https://github.com/75lb/local-web-server/tree/master/example/simple).
  40. ### Single Page Application
  41. You're building a web app with client-side routing, so mark `index.html` as the SPA.
  42. ```sh
  43. $ ws --spa index.html
  44. ```
  45. 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:
  46. *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.*
  47. [Example](https://github.com/75lb/local-web-server/tree/master/example/spa).
  48. ### URL rewriting
  49. Your application requested `/css/style.css` but it's stored at `/build/css/style.css`. To avoid a 404 you need a rewrite rule:
  50. ```sh
  51. $ ws --rewrite '/css/style.css -> /build/css/style.css'
  52. ```
  53. Or, more generally (matching any stylesheet under `/css`):
  54. ```sh
  55. $ ws --rewrite '/css/:stylesheet -> /build/css/:stylesheet'
  56. ```
  57. With a deep CSS directory structure it may be easier to mount the entire contents of `/build/css` to the `/css` path:
  58. ```sh
  59. $ ws --rewrite '/css/* -> /build/css/$1'
  60. ```
  61. this rewrites `/css/a` as `/build/css/a`, `/css/a/b/c` as `/build/css/a/b/c` etc.
  62. #### Proxied requests
  63. If the `to` URL contains a remote host, local-web-server will act as a proxy - fetching and responding with the remote resource.
  64. Mount the npm registry locally:
  65. ```sh
  66. $ ws --rewrite '/npm/* -> http://registry.npmjs.org/$1'
  67. ```
  68. Map local requests for repo data to the Github API:
  69. ```sh
  70. $ ws --rewrite '/:user/repos/:name -> https://api.github.com/repos/:user/:name'
  71. ```
  72. [Example](https://github.com/75lb/local-web-server/tree/master/example/rewrite).
  73. ### Mock Responses
  74. Mock a data service, serve any custom/dynamic content.
  75. A mock definition maps a route to a response. Mock a home page.
  76. ```json
  77. {
  78. "mocks": [
  79. {
  80. "route": "/",
  81. "response": {
  82. "body": "<h1>Welcome to the Mock Responses example</h1>"
  83. }
  84. }
  85. ]
  86. }
  87. ```
  88. Conditional response, depending on the request.
  89. ```json
  90. {
  91. "mocks": [
  92. {
  93. "route": "/two",
  94. "request": { "accepts": "xml" },
  95. "response": {
  96. "body": "<result id='2' name='whatever' />"
  97. }
  98. }
  99. ]
  100. }
  101. ```
  102. Multiple potential responses. First request to match.
  103. ```json
  104. {
  105. "mocks": [
  106. {
  107. "route": "/three",
  108. "responses": [
  109. {
  110. "request": { "method": "GET" },
  111. "response": {
  112. "body": "<h1>Mock response for 'GET' request on /three</h1>"
  113. }
  114. },
  115. {
  116. "request": { "method": "POST" },
  117. "response": {
  118. "status": 400,
  119. "body": { "message": "That method is not allowed." }
  120. }
  121. }
  122. ]
  123. }
  124. ]
  125. }
  126. ```
  127. More dynamic response.
  128. ```json
  129. {
  130. "mocks": [
  131. {
  132. "route": "/four",
  133. "module": "/mocks/four.js"
  134. }
  135. ]
  136. }
  137. ```
  138. Tokens in the route are passed to the response.
  139. ```json
  140. {
  141. "mocks": [
  142. {
  143. "route": "/five/:id\\?name=:name",
  144. "module": "/mocks/five.js"
  145. }
  146. ]
  147. }
  148. ```
  149. [Example](https://github.com/75lb/local-web-server/tree/master/example/mock).
  150. ### Stored config
  151. Use the same port and blacklist every time? Persist it to `package.json`:
  152. ```json
  153. {
  154. "name": "example",
  155. "version": "1.0.0",
  156. "local-web-server": {
  157. "port": 8100,
  158. "forbid": "*.json"
  159. }
  160. }
  161. ```
  162. or `.local-web-server.json`
  163. ```json
  164. {
  165. "port": 8100,
  166. "forbid": "*.json"
  167. }
  168. ```
  169. 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.
  170. To inspect stored config, run:
  171. ```sh
  172. $ ws --config
  173. ```
  174. ### Logging
  175. By default, local-web-server outputs a simple, dynamic statistics view. To see traditional web server logs, use `--log-format`:
  176. ```sh
  177. $ ws --log-format combined
  178. serving at http://localhost:8000
  179. ::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"
  180. ```
  181. The format value supplied is passed directly to [morgan](https://github.com/expressjs/morgan). The exception is `--log-format none` which disables all output.
  182. ### Access Control
  183. By default, access to all files is allowed (including dot files). Use `--forbid` to establish a blacklist:
  184. ```sh
  185. $ ws --forbid '*.json' '*.yml'
  186. serving at http://localhost:8000
  187. ```
  188. [Example](https://github.com/75lb/local-web-server/tree/master/example/forbid).
  189. ### Other usage
  190. #### Debugging
  191. Prints information about loaded middleware, arguments, remote proxy fetches etc.
  192. ```sh
  193. $ ws --verbose
  194. ```
  195. #### Compression
  196. Serve gzip-compressed resources, where applicable
  197. ```sh
  198. $ ws --compress
  199. ```
  200. #### Disable caching
  201. Disable etag response headers, forcing resources to be served in full every time.
  202. ```sh
  203. $ ws --no-cache
  204. ```
  205. #### mime-types
  206. 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:
  207. ```json
  208. {
  209. "mime": {
  210. "text/plain": [ "php", "pl" ]
  211. }
  212. }
  213. ```
  214. [Example](https://github.com/75lb/local-web-server/tree/master/example/mime-override).
  215. #### Log Visualisation
  216. 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).
  217. ## Install
  218. Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
  219. ```sh
  220. $ npm install -g local-web-server
  221. ```
  222. This will install the `ws` tool globally. To see the available options, run:
  223. ```sh
  224. $ ws --help
  225. ```
  226. ## Distribute with your project
  227. The standard convention with client-server applications is to add an `npm start` command to launch the server component.
  228. 1\. Install the server as a dev dependency
  229. ```sh
  230. $ npm install local-web-server --save-dev
  231. ```
  232. 2\. Add a `start` command to your `package.json`:
  233. ```json
  234. {
  235. "name": "example",
  236. "version": "1.0.0",
  237. "local-web-server": {
  238. "port": 8100,
  239. "forbid": "*.json"
  240. },
  241. "scripts": {
  242. "start": "ws"
  243. }
  244. }
  245. ```
  246. 3\. Document how to build and launch your site
  247. ```sh
  248. $ npm install
  249. $ npm start
  250. serving at http://localhost:8100
  251. ```
  252. ## API Reference
  253. {{#module name="local-web-server"}}
  254. {{>body~}}
  255. {{>member-index~}}
  256. {{>separator~}}
  257. {{>members~}}
  258. {{/module}}
  259. * * *
  260. &copy; 2015 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).