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.

425 lines
13 KiB

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