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.

198 lines
5.1 KiB

11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
12 years ago
11 years ago
10 years ago
11 years ago
12 years ago
10 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
11 years ago
11 years ago
11 years ago
12 years ago
10 years ago
11 years ago
10 years ago
11 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 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
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 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 per month](http://img.shields.io/npm/dm/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
  3. [![Dependency Status](https://david-dm.org/75lb/local-web-server.svg)](https://david-dm.org/75lb/local-web-server)
  4. ![Analytics](https://ga-beacon.appspot.com/UA-27725889-12/local-web-server/README.md?pixel)
  5. # local-web-server
  6. Fires up a simple, CORS-enabled, static web server on a given port. Use for local web development or file sharing (directory browsing enabled).
  7. ![local-web-server](http://75lb.github.io/local-web-server/ws.gif)
  8. ## Install
  9. Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
  10. ### Globally
  11. ```sh
  12. $ npm install -g local-web-server
  13. ```
  14. ### Bundled with your project
  15. ```sh
  16. $ npm install local-web-server --save-dev
  17. ```
  18. Then add an `start` script to your `package.json` (the standard npm approach):
  19. ```json
  20. {
  21. "name": "my-web-app",
  22. "version": "1.0.0",
  23. "scripts": {
  24. "start": "ws"
  25. }
  26. }
  27. ```
  28. This simplifies a rather specific-looking instruction set like:
  29. ```sh
  30. $ npm install
  31. $ npm install -g local-web-server
  32. $ ws
  33. ```
  34. to the following, server implementation and launch details abstracted away:
  35. ```sh
  36. $ npm install
  37. $ npm start
  38. ```
  39. ## Usage
  40. ```
  41. local-web-server
  42. Lightweight static web server, zero configuration.
  43. Usage
  44. $ ws <server options>
  45. $ ws --config
  46. $ ws --help
  47. Server
  48. -p, --port <number> Web server port
  49. -f, --log-format <string> If a format is supplied an access log is written to stdout. If not, a statistics view is displayed. Use a
  50. preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method ->
  51. :url').
  52. -d, --directory <string> Root directory, defaults to the current directory
  53. -c, --compress Enable gzip compression, reduces bandwidth.
  54. -r, --refresh-rate <number> Statistics view refresh rate in ms. Defaults to 500.
  55. Misc
  56. -h, --help Print these usage instructions
  57. --config Print the stored config
  58. Project home: https://github.com/75lb/local-web-server
  59. ```
  60. From the folder you wish to serve, run:
  61. ```sh
  62. $ ws
  63. serving at http://localhost:8000
  64. ```
  65. If you wish to serve a different directory, run:
  66. ```sh
  67. $ ws -d ~/mysite/
  68. serving /Users/Lloyd/mysite at http://localhost:8000
  69. ```
  70. If you wish to override the default port (8000), use `--port` or `-p`:
  71. ```sh
  72. $ ws --port 9000
  73. serving at http://localhost:9000
  74. ```
  75. To add compression, reducing bandwidth, increasing page load time (by 10-15% on my Macbook Air)
  76. ```sh
  77. $ ws --compress
  78. ```
  79. ### Logging
  80. Passing a value to `--log-format` will write an access log to `stdout`.
  81. Either use a built-in [morgan](https://github.com/expressjs/morgan) logger preset:
  82. ```sh
  83. $ ws --log-format short
  84. ```
  85. Or a custom [morgan](https://github.com/expressjs/morgan) log format:
  86. ```sh
  87. $ ws -f ':method -> :url'
  88. ```
  89. Or silence:
  90. ```sh
  91. $ ws -f none
  92. ```
  93. ## Storing default options
  94. 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`:
  95. ```json
  96. {
  97. "name": "my-project",
  98. "version": "0.11.8",
  99. "local-web-server":{
  100. "port": 8100
  101. }
  102. }
  103. ```
  104. Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site):
  105. ```json
  106. {
  107. "port": 8100,
  108. "log-format": "tiny"
  109. }
  110. ```
  111. Or store global defaults in a `.local-web-server.json` file in your home directory.
  112. ```json
  113. {
  114. "port": 3000,
  115. "refresh-rate": 1000
  116. }
  117. ```
  118. All stored defaults are overriden by options supplied at the command line.
  119. To view your stored defaults, run:
  120. ```sh
  121. $ ws --config
  122. ```
  123. ## mime-types
  124. 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:
  125. ```json
  126. {
  127. "mime": {
  128. "text/plain": [ "php", "pl" ]
  129. }
  130. }
  131. ```
  132. ## Use with Logstalgia
  133. local-web-server is compatible with [logstalgia](http://code.google.com/p/logstalgia/).
  134. ### Install Logstalgia
  135. On MacOSX, install with [homebrew](http://brew.sh):
  136. ```sh
  137. $ brew install logstalgia
  138. ```
  139. Alternatively, [download a release for your system from github](https://github.com/acaudwell/Logstalgia/releases/latest).
  140. Then pipe the `logstalgia` output format directly into logstalgia for real-time visualisation:
  141. ```sh
  142. $ ws -f logstalgia | logstalgia -
  143. ```
  144. ![local-web-server with logstalgia](http://75lb.github.io/local-web-server/logstagia.gif)
  145. ## Use with glTail
  146. To use with [glTail](http://www.fudgie.org), write your log to disk using the "default" format:
  147. ```sh
  148. $ ws -f default > web.log
  149. ```
  150. Then specify this file in your glTail config:
  151. ```yaml
  152. servers:
  153. dev:
  154. host: localhost
  155. source: local
  156. files: /Users/Lloyd/Documents/MySite/web.log
  157. parser: apache
  158. color: 0.2, 0.2, 1.0, 1.0
  159. ```
  160. &copy; 2015 Lloyd Brookes <75pound@gmail.com>