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.

201 lines
5.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. For the examples below, assume we're in a project 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` as there is no file at that location on disk. By marking `index.html` as the SPA you create this rule:
  30. *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.*
  31. ### Access Control
  32. Access to all files is allowed, beside those you forbid (e.g. config files):
  33. ```sh
  34. $ ws --forbid .json .yml
  35. serving at http://localhost:8000
  36. ```
  37. ### URL rewriting
  38. When urls don't map to your directory structure, rewrite:
  39. ```sh
  40. $ ws --rewrite /css=>/build/css
  41. ```
  42. Rewrite to remote servers (proxy):
  43. ```sh
  44. $ ws --rewrite "/api => http://api.example.com/api" \
  45. "/npm => http://registry.npmjs.com" \
  46. "/user/:project/repo -> https://api.github.com/repos/:project"
  47. ```
  48. ### Mock Responses
  49. *Coming soon*.
  50. ### Stored config
  51. Always use this port and blacklist? Persist it to the config:
  52. ```json
  53. {
  54. "name": "example",
  55. "version": "1.0.0",
  56. "local-web-server": {
  57. "port": 8100,
  58. "forbid": "\\.json$"
  59. }
  60. }
  61. ```
  62. ### Logging
  63. By default, local-web-server outputs a simple, dynamic statistics view. To see traditional web server logs, use `--log-format`:
  64. ```sh
  65. $ ws --log-format combined
  66. serving at http://localhost:8000
  67. ::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"
  68. ```
  69. ### Other features
  70. Compression, caching, simple statistics view, log, override mime types.
  71. ## Tips
  72. ### Use with Google DevTools Workspaces
  73. ### Log Visualisation
  74. Instructions for how to visualise log output using goaccess, logstalgia or gltail [here](https://github.com/75lb/local-web-server/wiki/Log-visualisation).
  75. ## Install
  76. Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
  77. ```sh
  78. $ npm install -g local-web-server
  79. ```
  80. This will install the `ws` tool globally. To see the available options, run:
  81. ```sh
  82. $ ws --help
  83. ```
  84. ## Distribute with your project
  85. The standard convention with client-server applications is to add an `npm start` command to launch the server component.
  86. 1\. Install the server as a dev dependency
  87. ```sh
  88. $ npm install local-web-server --save-dev
  89. ```
  90. 2\. Add a `start` command to your `package.json`:
  91. ```json
  92. {
  93. "name": "example",
  94. "version": "1.0.0",
  95. "local-web-server": {
  96. "port": 8100,
  97. "forbid": "\\.json$"
  98. },
  99. "scripts": {
  100. "start": "ws"
  101. }
  102. }
  103. ```
  104. 3\. Document how to build and launch your site
  105. ```sh
  106. $ npm install
  107. $ npm start
  108. serving at http://localhost:8100
  109. ```
  110. ## Storing default options
  111. 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`:
  112. ```json
  113. {
  114. "name": "my-project",
  115. "version": "0.11.8",
  116. "local-web-server":{
  117. "port": 8100
  118. }
  119. }
  120. ```
  121. Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site):
  122. ```json
  123. {
  124. "port": 8100,
  125. "log-format": "tiny"
  126. }
  127. ```
  128. Or store global defaults in a `.local-web-server.json` file in your home directory.
  129. ```json
  130. {
  131. "port": 3000,
  132. "refresh-rate": 1000
  133. }
  134. ```
  135. All stored defaults are overriden by options supplied at the command line.
  136. To view your stored defaults, run:
  137. ```sh
  138. $ ws --config
  139. ```
  140. ## mime-types
  141. 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:
  142. ```json
  143. {
  144. "mime": {
  145. "text/plain": [ "php", "pl" ]
  146. }
  147. }
  148. ```
  149. ## API Reference
  150. {{>main}}
  151. ## Composition
  152. * * *
  153. &copy; 2015 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).