2014-05-01 11:28:15 +01:00
[](https://www.npmjs.org/package/local-web-server)
2015-11-15 21:15:25 +00:00
[](https://www.npmjs.org/package/local-web-server)
[](https://travis-ci.org/75lb/local-web-server)
2014-05-08 15:05:06 +02:00
[](https://david-dm.org/75lb/local-web-server)
2015-10-30 11:31:59 +00:00
[](https://github.com/feross/standard)
2015-12-15 22:33:11 +00:00
[](https://gitter.im/75lb/local-web-server?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2014-01-24 19:49:11 +01:00
2015-11-30 17:52:46 +00:00
***Requires node v4.0.0 or higher. Install the [previous release ](https://github.com/75lb/local-web-server/tree/prev ) for older node support.***
2015-11-20 09:52:51 +00:00
2015-02-01 14:04:17 +00:00
# local-web-server
2016-06-20 20:32:32 +01:00
An application shell for building a simple, command-line web server for productive web development.
2015-11-26 21:12:49 +00:00
2016-06-20 20:32:32 +01:00
It is trivial is bundle and deploy with your project. Also deploys to heroku well for demo projects.
It comes with some middleware built-in, which you need not use but will get you up and running for the following use cases:
* Static or Single Page Application front-end development where you have
* No backend, an existing remote API or need to mock-up an API.
Application Shell
* HTTP or HTTPS server
* HTTPS is strictly required by some modern techs (ServiceWorker, Media Capture and Streams etc.)
* Add your middleware
* Use any combination of built-in and custom middleware
* specify options (for command line or config)
* Accepts Koa v1 or 2 middleware
2015-11-26 21:12:49 +00:00
* Bundle with your front-end project
2016-06-20 20:32:32 +01:00
* Configuration is via json file or command-line (latter taking presedence)
2015-11-26 21:12:49 +00:00
* Outputs a dynamic statistics view to the terminal
2016-06-20 20:32:32 +01:00
Built-in Middleware (all optional)
* Rewrite routes to local or remote resources
* Efficient, predictable, entity-tag-powered conditional request handling (no need to 'Disable Cache' in DevTools, slowing page-load down)
2015-11-26 21:12:49 +00:00
* Configurable log output, compatible with [Goaccess, Logstalgia and glTail ](https://github.com/75lb/local-web-server/blob/master/doc/visualisation.md )
2016-06-20 20:32:32 +01:00
* Proxy server
* Map local routes to remote servers. Removes CORS pain when consuming remote services.
* Back-end service mocking
* Prototype a web service, microservice, REST API etc.
* Mocks are defined with config (static), or code (dynamic).
2015-11-26 21:12:49 +00:00
* CORS-friendly, all origins allowed by default.
2013-06-04 14:23:50 +01:00
2015-11-15 21:15:25 +00:00
## Synopsis
2016-06-20 20:32:32 +01:00
local-web-server is a command-line tool. To serve the current directory, run `ws` .
2015-11-27 09:31:28 +00:00
2015-11-27 09:40:53 +00:00
< pre > < code > $ ws --help
2015-11-27 09:31:28 +00:00
< strong > local-web-server< / strong >
A simple web-server for productive front-end development.
2015-11-27 09:36:46 +00:00
< strong > Synopsis< / strong >
2015-11-27 09:31:28 +00:00
2015-11-27 09:40:53 +00:00
$ ws [< server options> ]
2015-11-27 09:31:28 +00:00
$ ws --config
$ ws --help
2015-11-27 09:36:46 +00:00
< strong > Server< / strong >
2015-11-27 09:31:28 +00:00
-p, --port number Web server port.
-d, --directory path Root directory, defaults to the current directory.
2015-11-27 09:36:46 +00:00
-f, --log-format string If a format is supplied an access log is written to stdout. If
not, a dynamic statistics view is displayed. Use a preset ('none',
'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a
custom format (e.g. ':method -> :url').
-r, --rewrite expression ... A list of URL rewrite rules. For each rule, separate the 'from'
and 'to' routes with '->'. Whitespace surrounded the routes is
ignored. E.g. '/from -> /to'.
2015-11-27 09:31:28 +00:00
-s, --spa file Path to a Single Page App, e.g. app.html.
-c, --compress Serve gzip-compressed resources, where applicable.
-b, --forbid path ... A list of forbidden routes.
2015-11-27 09:36:46 +00:00
-n, --no-cache Disable etag-based caching -forces loading from disk each request.
2015-12-01 17:17:02 +00:00
--key file SSL key. Supply along with --cert to launch a https server.
--cert file SSL cert. Supply along with --key to launch a https server.
2016-03-10 16:21:13 +00:00
--https Enable HTTPS using a built-in key and cert, registered to the
domain 127.0.0.1.
2015-11-27 09:31:28 +00:00
--verbose Verbose output, useful for debugging.
2015-11-27 09:36:46 +00:00
< strong > Misc< / strong >
2015-11-27 09:31:28 +00:00
-h, --help Print these usage instructions.
--config Print the stored config.
Project home: https://github.com/75lb/local-web-server
< / code > < / pre >
## Examples
2015-11-17 15:13:22 +00:00
For the examples below, we assume we're in a project directory looking like this:
2015-11-15 21:15:25 +00:00
```sh
2015-11-15 23:52:13 +00:00
.
├── css
│ └── style.css
2015-11-16 10:30:04 +00:00
├── index.html
└── package.json
```
2015-11-15 23:52:13 +00:00
2016-06-20 20:32:32 +01:00
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` .
2015-11-17 14:06:45 +00:00
2015-11-16 10:30:04 +00:00
### Static site
Fire up your static site on the default port:
```sh
2015-11-15 21:15:25 +00:00
$ ws
serving at http://localhost:8000
```
2015-11-19 10:01:04 +00:00
[Example ](https://github.com/75lb/local-web-server/tree/master/example/simple ).
2015-11-17 14:02:23 +00:00
### Other usage
2015-11-17 12:55:25 +00:00
2015-11-17 17:50:37 +00:00
#### Debugging
Prints information about loaded middleware, arguments, remote proxy fetches etc.
```sh
$ ws --verbose
```
2015-11-17 14:02:23 +00:00
#### Compression
2015-11-16 10:30:04 +00:00
2015-11-17 13:49:12 +00:00
Serve gzip-compressed resources, where applicable
```sh
$ ws --compress
```
2015-11-16 11:23:53 +00:00
2015-11-17 14:02:23 +00:00
#### Disable caching
2015-11-17 14:06:45 +00:00
Disable etag response headers, forcing resources to be served in full every time.
2015-11-17 13:49:12 +00:00
```sh
$ ws --no-cache
```
2015-11-16 11:23:53 +00:00
2015-11-17 14:02:23 +00:00
#### Log Visualisation
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 ).
2015-11-16 11:23:53 +00:00
2015-02-01 14:04:17 +00:00
## Install
2014-06-17 13:37:51 +01:00
Ensure [node.js ](http://nodejs.org ) is installed first. Linux/Mac users may need to run the following commands with `sudo` .
2013-06-04 14:23:50 +01:00
```sh
$ npm install -g local-web-server
```
2015-11-15 23:00:17 +00:00
## Distribute with your project
2015-11-16 10:51:32 +00:00
The standard convention with client-server applications is to add an `npm start` command to launch the server component.
2015-11-16 11:23:53 +00:00
1\. Install the server as a dev dependency
2015-11-16 10:51:32 +00:00
2014-06-17 13:37:51 +01:00
```sh
$ npm install local-web-server --save-dev
```
2015-11-16 11:23:53 +00:00
2\. Add a `start` command to your `package.json` :
2015-11-16 10:51:32 +00:00
2014-06-17 13:37:51 +01:00
```json
2014-06-17 13:42:48 +01:00
{
2015-11-16 10:51:32 +00:00
"name": "example",
2014-06-17 13:42:48 +01:00
"version": "1.0.0",
2015-11-16 10:51:32 +00:00
"local-web-server": {
"port": 8100,
2015-11-19 16:15:34 +00:00
"forbid": "*.json"
2015-11-16 10:51:32 +00:00
},
2014-06-17 13:37:51 +01:00
"scripts": {
"start": "ws"
}
2014-06-17 13:49:46 +01:00
}
2014-06-17 13:37:51 +01:00
```
2015-11-16 11:23:53 +00:00
3\. Document how to build and launch your site
2014-06-17 13:37:51 +01:00
```sh
$ npm install
$ npm start
2015-11-16 11:23:53 +00:00
serving at http://localhost:8100
2014-06-17 13:37:51 +01:00
```
2013-06-04 14:23:50 +01:00
2015-11-08 22:09:07 +00:00
* * *
2016-03-10 10:58:52 +00:00
© 2013-16 Lloyd Brookes < 75pound @gmail .com > . Documented by [jsdoc-to-markdown ](https://github.com/jsdoc2md/jsdoc-to-markdown ).