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.

196 lines
4.8 KiB

9 years ago
11 years ago
11 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
10 years ago
11 years ago
12 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
12 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 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
11 years ago
10 years ago
10 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. # local-web-server
  7. A simple web-server for productive front-end development.
  8. **Requires node v4.0.0 or higher**.
  9. ## Synopsis
  10. Some typical use cases. For these examples, assume we're in a site directory looking like this:
  11. ```sh
  12. .
  13. ├── css
  14. │   └── style.css
  15. ├── index.html
  16. └── package.json
  17. ```
  18. ### Static site
  19. Fire up your static site on the default port:
  20. ```sh
  21. $ ws
  22. serving at http://localhost:8000
  23. ```
  24. ### Single Page Application
  25. You're building a web app with client-side routing, so mark `index.html` as the SPA.
  26. ```sh
  27. $ ws --spa index.html
  28. ```
  29. By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found`. By marking `index.html` as the SPA you create this rule: *if a file with that url exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then pass it to the SPA*.
  30. ### Access Control
  31. Access to all files is allowed, beside those you forbid (e.g. config files):
  32. ```sh
  33. $ ws --forbid .json .yml
  34. serving at http://localhost:8000
  35. ```
  36. ### URL rewriting
  37. When urls don't map to your directory structure, rewrite:
  38. ```sh
  39. $ ws --rewrite /css=>/build/css
  40. ```
  41. Rewrite to remote servers (proxy):
  42. ```sh
  43. $ ws --rewrite "/api => http://api.example.com/api" \
  44. "/npm => http://registry.npmjs.com" \
  45. "/user/:project/repo -> https://api.github.com/repos/:project"
  46. ```
  47. ### Mock Responses
  48. ### Stored config
  49. Always use this port and blacklist? Persist it to the config:
  50. ```json
  51. {
  52. "name": "example",
  53. "version": "1.0.0",
  54. "local-web-server": {
  55. "port": 8100,
  56. "forbid": "\\.json$"
  57. }
  58. }
  59. ```
  60. ### Other features
  61. Compression, caching, simple statistics view, log, override mime types.
  62. ## Install
  63. Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
  64. ```sh
  65. $ npm install -g local-web-server
  66. ```
  67. ## Distribute with your project
  68. The standard convention with client-server applications is to add an `npm start` command to launch the server component.
  69. 1. Install the server as a dev dependency
  70. ```sh
  71. $ npm install local-web-server --save-dev
  72. ```
  73. 2. Add a `start` command to your `package.json`:
  74. ```json
  75. {
  76. "name": "example",
  77. "version": "1.0.0",
  78. "local-web-server": {
  79. "port": 8100,
  80. "forbid": "\\.json$"
  81. },
  82. "scripts": {
  83. "start": "ws"
  84. }
  85. }
  86. ```
  87. 3. Document how to build and launch your site
  88. ```sh
  89. $ npm install
  90. $ npm start
  91. ```
  92. ## Storing default options
  93. To store per-project options, saving you the hassle of inputting them everytime, store them in the `local-web-server` property of your project's `package.json`:
  94. ```json
  95. {
  96. "name": "my-project",
  97. "version": "0.11.8",
  98. "local-web-server":{
  99. "port": 8100
  100. }
  101. }
  102. ```
  103. Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site):
  104. ```json
  105. {
  106. "port": 8100,
  107. "log-format": "tiny"
  108. }
  109. ```
  110. Or store global defaults in a `.local-web-server.json` file in your home directory.
  111. ```json
  112. {
  113. "port": 3000,
  114. "refresh-rate": 1000
  115. }
  116. ```
  117. All stored defaults are overriden by options supplied at the command line.
  118. To view your stored defaults, run:
  119. ```sh
  120. $ ws --config
  121. ```
  122. ## mime-types
  123. You can set additional mime-type/extension mappings, or override the defaults by setting a `mime` value in your local config. This value is passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine). Example:
  124. ```json
  125. {
  126. "mime": {
  127. "text/plain": [ "php", "pl" ]
  128. }
  129. }
  130. ```
  131. ## Use with Google DevTools Workspaces
  132. ## Log Visualisation
  133. Instructions for how to visualise log output using goaccess, logstalgia or gltail [here](https://github.com/75lb/local-web-server/wiki/Log-visualisation).
  134. ## API Reference
  135. <a name="module_local-web-server"></a>
  136. ## local-web-server
  137. <a name="exp_module_local-web-server--localWebServer"></a>
  138. ### localWebServer([options]) ⏏
  139. Returns a Koa application
  140. **Kind**: Exported function
  141. | Param | Type | Description |
  142. | --- | --- | --- |
  143. | [options] | <code>object</code> | options |
  144. | [options.forbid] | <code>Array.&lt;regexp&gt;</code> | a list of forbidden routes. |
  145. **Example**
  146. ```js
  147. const localWebServer = require('local-web-server')
  148. ```
  149. * * *
  150. &copy; 2015 Lloyd Brookes <75pound@gmail.com>