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.

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