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.

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