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.

195 lines
5.0 KiB

  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/lwsjs/local-web-server.svg)](https://david-dm.org/lwsjs/local-web-server)
  4. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
  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. Usage
  42. $ ws <server options>
  43. $ ws --config
  44. $ ws --help
  45. Server
  46. -p, --port <number> Web server port
  47. -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
  48. preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method ->
  49. :url').
  50. -d, --directory <string> Root directory, defaults to the current directory
  51. -c, --compress Enable gzip compression, reduces bandwidth.
  52. -r, --refresh-rate <number> Statistics view refresh rate in ms. Defaults to 500.
  53. Misc
  54. -h, --help Print these usage instructions
  55. --config Print the stored config
  56. ```
  57. From the folder you wish to serve, run:
  58. ```sh
  59. $ ws
  60. serving at http://localhost:8000
  61. ```
  62. If you wish to serve a different directory, run:
  63. ```sh
  64. $ ws -d ~/mysite/
  65. serving /Users/Lloyd/mysite at http://localhost:8000
  66. ```
  67. If you wish to override the default port (8000), use `--port` or `-p`:
  68. ```sh
  69. $ ws --port 9000
  70. serving at http://localhost:9000
  71. ```
  72. To add compression, reducing bandwidth, increasing page load time (by 10-15% on my Macbook Air)
  73. ```sh
  74. $ ws --compress
  75. ```
  76. ### Logging
  77. Passing a value to `--log-format` will write an access log to `stdout`.
  78. Either use a built-in [morgan](https://github.com/expressjs/morgan) logger preset:
  79. ```sh
  80. $ ws --log-format short
  81. ```
  82. Or a custom [morgan](https://github.com/expressjs/morgan) log format:
  83. ```sh
  84. $ ws -f ':method -> :url'
  85. ```
  86. Or silence:
  87. ```sh
  88. $ ws -f none
  89. ```
  90. ## Storing default options
  91. 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`:
  92. ```json
  93. {
  94. "name": "my-project",
  95. "version": "0.11.8",
  96. "local-web-server":{
  97. "port": 8100
  98. }
  99. }
  100. ```
  101. Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site):
  102. ```json
  103. {
  104. "port": 8100,
  105. "log-format": "tiny"
  106. }
  107. ```
  108. Or store global defaults in a `.local-web-server.json` file in your home directory.
  109. ```json
  110. {
  111. "port": 3000,
  112. "refresh-rate": 1000
  113. }
  114. ```
  115. All stored defaults are overriden by options supplied at the command line.
  116. To view your stored defaults, run:
  117. ```sh
  118. $ ws --config
  119. ```
  120. ## mime-types
  121. 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:
  122. ```json
  123. {
  124. "mime": {
  125. "text/plain": [ "php", "pl" ]
  126. }
  127. }
  128. ```
  129. ## Use with Logstalgia
  130. local-web-server is compatible with [logstalgia](http://code.google.com/p/logstalgia/).
  131. ### Install Logstalgia
  132. On MacOSX, install with [homebrew](http://brew.sh):
  133. ```sh
  134. $ brew install logstalgia
  135. ```
  136. Alternatively, [download a release for your system from github](https://github.com/acaudwell/Logstalgia/releases/latest).
  137. Then pipe the `logstalgia` output format directly into logstalgia for real-time visualisation:
  138. ```sh
  139. $ ws -f logstalgia | logstalgia -
  140. ```
  141. ![local-web-server with logstalgia](http://75lb.github.io/local-web-server/logstagia.gif)
  142. ## Use with glTail
  143. To use with [glTail](http://www.fudgie.org), write your log to disk using the "default" format:
  144. ```sh
  145. $ ws -f default > web.log
  146. ```
  147. Then specify this file in your glTail config:
  148. ```yaml
  149. servers:
  150. dev:
  151. host: localhost
  152. source: local
  153. files: /Users/Lloyd/Documents/MySite/web.log
  154. parser: apache
  155. color: 0.2, 0.2, 1.0, 1.0
  156. ```
  157. * * *
  158. &copy; 2015 Lloyd Brookes <75pound@gmail.com>