Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: lwsjs/local-web-server
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 71fb4a9375d5c9fbb678d558b8740e92f56edd70
Choose a base ref
...
head repository: lwsjs/local-web-server
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5ac6cd1cd6d86adb9c657b70dd82162cbf0194ba
Choose a head ref

Commits on Dec 27, 2018

  1. upgrade deps and travis

    75lb committed Dec 27, 2018
    Copy the full SHA
    be7dc86 View commit details
  2. 2.6.1

    75lb committed Dec 27, 2018
    Copy the full SHA
    cd9d5ed View commit details

Commits on May 23, 2019

  1. update dates, deps and travis

    75lb committed May 23, 2019
    Copy the full SHA
    7ad2f11 View commit details

Commits on May 27, 2019

  1. Copy the full SHA
    5d5c902 View commit details
  2. 3.0.0-0

    75lb committed May 27, 2019
    Copy the full SHA
    37153b6 View commit details
  3. fix lws version, for now

    75lb committed May 27, 2019
    Copy the full SHA
    16696b6 View commit details
  4. 3.0.0-1

    75lb committed May 27, 2019
    Copy the full SHA
    bba4314 View commit details

Commits on Jun 2, 2019

  1. upgrade to the latest lws

    75lb committed Jun 2, 2019
    Copy the full SHA
    4c2fc87 View commit details

Commits on Jun 3, 2019

  1. upgrade all plugins

    75lb committed Jun 3, 2019
    Copy the full SHA
    d6146e3 View commit details
  2. 3.0.0-2

    75lb committed Jun 3, 2019
    Copy the full SHA
    76e2a27 View commit details
  3. Copy the full SHA
    9bec6dc View commit details

Commits on Jun 4, 2019

  1. upgrade lws

    75lb committed Jun 4, 2019
    Copy the full SHA
    7588a97 View commit details
  2. 3.0.0-3

    75lb committed Jun 4, 2019
    Copy the full SHA
    f1c50e1 View commit details

Commits on Jun 6, 2019

  1. deps, readme, remove API docs

    75lb committed Jun 6, 2019
    Copy the full SHA
    69c2def View commit details
  2. fix test

    75lb committed Jun 6, 2019
    Copy the full SHA
    5f7f450 View commit details

Commits on Jun 7, 2019

  1. docs

    75lb committed Jun 7, 2019
    Copy the full SHA
    3139339 View commit details

Commits on Jun 8, 2019

  1. readme

    75lb committed Jun 8, 2019
    Copy the full SHA
    cc71999 View commit details
  2. readme

    75lb committed Jun 8, 2019
    Copy the full SHA
    c9a6101 View commit details
  3. readme

    75lb committed Jun 8, 2019
    Copy the full SHA
    1998b90 View commit details
  4. readme

    75lb committed Jun 8, 2019
    Copy the full SHA
    329040e View commit details
  5. description

    75lb committed Jun 8, 2019
    Copy the full SHA
    d55ca0f View commit details
  6. 3.0.0-4

    75lb committed Jun 8, 2019
    Copy the full SHA
    f209496 View commit details

Commits on Jun 9, 2019

  1. deps, docs

    75lb committed Jun 9, 2019
    Copy the full SHA
    0dc511a View commit details
  2. docs

    75lb committed Jun 9, 2019
    Copy the full SHA
    9277ad6 View commit details
  3. readme

    75lb committed Jun 9, 2019
    Copy the full SHA
    1f86cb7 View commit details
  4. 3.0.0

    75lb committed Jun 9, 2019
    Copy the full SHA
    5ac6cd1 View commit details
Showing with 2,051 additions and 1,228 deletions.
  1. +1 −2 .travis.yml
  2. +2 −1 LICENSE
  3. +57 −101 README.md
  4. +5 −3 bin/cli.js
  5. +0 −51 doc/api.md
  6. +4 −47 index.js
  7. +57 −9 lib/cli-app.js
  8. +0 −11 lib/command/middleware-list.js
  9. +0 −44 lib/command/serve.js
  10. +0 −1 lib/default-stack.js
  11. +1,855 −900 package-lock.json
  12. +34 −31 package.json
  13. +27 −17 test/cli.js
  14. +9 −10 test/test.js
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- 7
- 8
- 9
- 10
- 12
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) 2013-18 Lloyd Brookes <75pound@gmail.com>
Copyright (c) 2013-19
Lloyd Brookes <75pound@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
158 changes: 57 additions & 101 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,165 +1,121 @@
[![npm (tag)](https://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
[![view on npm](https://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
[![npm module downloads](https://img.shields.io/npm/dt/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
[![Build Status](https://travis-ci.org/lwsjs/local-web-server.svg?branch=master)](https://travis-ci.org/lwsjs/local-web-server)
[![Coverage Status](https://coveralls.io/repos/github/lwsjs/local-web-server/badge.svg?branch=master)](https://coveralls.io/github/lwsjs/local-web-server?branch=master)
[![dependencies Status](https://david-dm.org/lwsjs/local-web-server/master/status.svg)](https://david-dm.org/lwsjs/local-web-server/master)
[![Dependency Status](https://badgen.net/david/dep/lwsjs/local-web-server)](https://david-dm.org/lwsjs/local-web-server)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
[![Join the chat at https://gitter.im/lwsjs/local-web-server](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/lwsjs/local-web-server?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

**Requires node v7.6 or above. Upgraders, please read the [release notes](https://github.com/lwsjs/local-web-server/releases)**.
*Requires node v8 or above. Upgraders, please read the [release notes](https://github.com/lwsjs/local-web-server/releases)*.

# local-web-server

The modular web server for productive full-stack development.
A lean, modular web server for rapid full-stack development.

Use this tool to:
* Supports HTTP, HTTPS and HTTP2.
* Small and 100% personalisable. Load and use only the behaviour required by your project.
* Attach a custom view to personalise how activity is visualised.
* Programmatic and command-line APIs.

* Build any flavour of web application (static site, dynamic site with client or server-rendered content, Single Page App, Progessive Web App, Angular or React app etc.)
* Prototype any CORS-enabled back-end service (e.g. RESTful HTTP API or Microservice using websockets, Server Sent Events etc.)
* Monitor activity, analyse performance, experiment with caching strategies etc.
* Build your own, personalised CLI web server tool
Use this tool to:

Features:
* Build any type of front-end web application (static, dynamic, Single Page App, Progessive Web App, React etc).
* Prototype a back-end service (REST API, microservice, websocket, Server Sent Events service etc).
* Monitor activity, analyse performance, experiment with caching strategy etc.

* Modular, extensible and easy to personalise. Create, share and consume only plugins which match your requirements.
* Powerful, extensible command-line interface (add your own commands and options)
* HTTP, HTTPS and HTTP2 support (HTTP2 requires node v8.4.0 or above)
* URL Rewriting to local or remote destinations
* Single Page Application support
* Response mocking
* Configurable access log
* Route blacklisting
* HTTP Conditional and Range request support
* Gzip response compression, HTTP Basic Authentication and much more
Local-web-server is a distribution of [lws](https://github.com/lwsjs/lws) bundled with a "starter pack" of useful middleware.

## Synopsis

This package installs the `ws` command-line tool (take a look at the [usage guide](https://github.com/lwsjs/local-web-server/wiki/CLI-usage)).

### Static web site

The most simple use case is to run `ws` without any arguments - this will **host the current directory as a static web site**. Navigating to the server will render a directory listing or your `index.html`, if that file exists.
Running `ws` without any arguments will host the current directory as a static web site. Navigating to the server will render a directory listing or your `index.html`, if that file exists.

```sh
$ ws
Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
Listening on http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
```

This clip demonstrates static hosting plus a couple of log output formats - `dev` and `stats`.

<img src="https://imgur.com/download/NJC3POY" width="618px" title="Static static log output">


### Single Page Application

Serving a Single Page Application (an app with client-side routing, e.g. a React or Angular app) is as trivial as specifying the name of your single page:

```sh
$ ws --spa index.html
Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
```

By default, requests for typical SPA paths (e.g. `/user/1`, `/login`) return `404 Not Found` as a file at that location does not exist. By marking `index.html` as the SPA you create this rule:
With a static site, requests for typical SPA paths (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file at that location does not exist. However, by marking `index.html` as the SPA you create this rule:

*If a static file is requested (e.g. `/css/style.css`) then serve it, if not (e.g. `/login`) then serve the specified SPA and handle the route client-side.*

[Read more](https://github.com/lwsjs/local-web-server/wiki/How-to-serve-a-Single-Page-Application-(SPA)).
[SPA tutorial](https://github.com/lwsjs/local-web-server/wiki/How-to-serve-a-Single-Page-Application-(SPA)).

### URL rewriting and proxied requests

Another common use case is to **re-route certain requests to a remote server** if, for example, you'd like to use data from a different environment. The following command would proxy requests with a URL beginning with `http://127.0.0.1:8000/api/` to `https://internal-service.local/api/`:
<img src="https://imgur.com/download/IQVmi8v" title="SPA">

```sh
$ ws --rewrite '/api/* -> https://internal-service.local/api/$1'
Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
```
### URL rewriting and proxied requests

### HTTPS
Another common use case is to forward certain requests to a remote server.

Launching a secure server is as simple as setting the `--https` flag. [See the wiki](https://github.com/lwsjs/local-web-server/wiki) for further configuration options and a guide on how to get the "green padlock" in your browser.
The following command proxies blog post requests from any path beginning with `/posts/` to `https://jsonplaceholder.typicode.com/posts/`. For example, a request for `/posts/1` would be proxied to `https://jsonplaceholder.typicode.com/posts/1`.

```sh
$ ws --https
Serving at https://mbp.local:8000, https://127.0.0.1:8000, https://192.168.0.100:8000
$ ws --rewrite '/posts/(.*) -> https://jsonplaceholder.typicode.com/posts/$1'
```

### HTTP2
[Rewrite tutorial](https://github.com/lwsjs/local-web-server/wiki/How-to-rewrite-URLs-to-local-or-remote-destinations).

Uses node's built-in HTTP2 support. HTTP2 servers are always secure using local-web-server's built-in SSL certificates (by default) or those supplied by `--cert`, `--key` or `--pfx`. [See the wiki](https://github.com/lwsjs/local-web-server/wiki) for further info about HTTPS options and a guide on how to get the "green padlock" in your browser.
This clip demonstrates the above plus use of `--static.extensions` to specify a default file extension and `--verbose` to monitor activity.

```sh
$ ws --http2
Serving at https://mbp.local:8000, https://127.0.0.1:8000, https://192.168.0.100:8000
```
<img src="https://imgur.com/download/3flcbJn" width="618px" title="Proxy json requests to remote resource">

### Mock responses

Imagine the network is down or you're working offline, proxied requests to `https://internal-service.local/api/users/1` would fail. In this case, Mock Responses can fill the gap. Mocks are defined in a module which can be reused between projects.

Trivial example - respond to a request for `/rivers` with some JSON. Save the following Javascript in a file named `example-mocks.js`.

```js
module.exports = MockBase => class MockRivers extends MockBase {
mocks () {
return {
route: '/rivers',
responses: [
{
response: {
type: 'json',
body: [
{ name: 'Volga', drainsInto: 'Caspian Sea' },
{ name: 'Danube', drainsInto: 'Black Sea' },
{ name: 'Ural', drainsInto: 'Caspian Sea' },
{ name: 'Dnieper', drainsInto: 'Black Sea' }
]
}
}
]
}
}
}
```
### HTTPS and HTTP2

Launch `ws` passing in your mocks module.
For HTTPS or HTTP2, pass the `--https` or `--http2` flags respectively. [See the wiki](https://github.com/lwsjs/local-web-server/wiki) for further configuration options and a guide on how to get the "green padlock" in your browser.

```sh
$ ws --mocks example-mocks.js
Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
```

GET your rivers.

```sh
$ curl http://127.0.0.1:8000/rivers
[
{
"name": "Volga",
"drainsInto": "Caspian Sea"
},
{
"name": "Danube",
"drainsInto": "Black Sea"
},
{
"name": "Ural",
"drainsInto": "Caspian Sea"
},
{
"name": "Dnieper",
"drainsInto": "Black Sea"
}
]
$ lws --http2
Listening at https://mba4.local:8000, https://127.0.0.1:8000, https://192.168.0.200:8000
```

See [the tutorials](https://github.com/lwsjs/local-web-server/wiki#tutorials) for more information and examples about mock responses.
## Built-in middleware stack

If you do *not* supply a custom middleware stack via the `--stack` option the following default stack will be used. It's designed to cover most typical web development scenarios.

| Name | Description |
| ------------------ | ---- |
|[Basic Auth](https://github.com/lwsjs/basic-auth) | Password-protect a server using Basic Authentication |
|[Body Parser](https://github.com/lwsjs/body-parser) | Parses the request body, making `ctx.request.body` available to downstream middleware.|
|[Request Monitor](https://github.com/lwsjs/request-monitor) | Feeds traffic information to the `--verbose` output.|
|[Log](https://github.com/lwsjs/log) | Outputs an access log or stats view to the console.|
|[Cors](https://github.com/lwsjs/cors) | Support for setting Cross-Origin Resource Sharing (CORS) headers |
|[Json](https://github.com/lwsjs/json) | Pretty-prints JSON responses. |
|[Rewrite](https://github.com/lwsjs/rewrite) | URL Rewriting. Use to re-route requests to local or remote destinations.|
|[Blacklist](https://github.com/lwsjs/blacklist) | Forbid access to sensitive or private resources|
|[Conditional Get](https://github.com/lwsjs/conditional-get) | Support for HTTP Conditional requests.|
|[Mime](https://github.com/lwsjs/mime) | Customise the mime-type returned with any static resource.|
|[Compress](https://github.com/lwsjs/compress) | Compress responses using gzip.|
|[SPA](https://github.com/lwsjs/spa) | Support for Single Page Applications.|
|[Static](https://github.com/lwsjs/static) | Serves static files.|
|[Index](https://github.com/lwsjs/index) | Serves directory listings.|

## Further Documentation

[See the wiki for plenty more documentation and tutorials](https://github.com/lwsjs/local-web-server/wiki).

## Install

**Requires node v7.6 or above**. Install the [previous release](https://github.com/lwsjs/local-web-server/tree/v1.x) for node >= v4.0.0.
**Requires node v8 or above**.

```sh
$ npm install -g local-web-server
```

* * *

&copy; 2013-18 Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
&copy; 2013-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
8 changes: 5 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node
const nodeVersionMatches = require('node-version-matches')

if (nodeVersionMatches('>=7.6.0')) {
require('../lib/cli-app').run()
if (nodeVersionMatches('>=8')) {
const WsCli = require('../lib/cli-app')
const cli = new WsCli()
cli.start()
} else {
console.log('Sorry, this app requires node v7.6.0 or above. Please upgrade https://nodejs.org/en/')
console.log('Sorry, this app requires node v8.0.0 or above. Please upgrade https://nodejs.org/en/')
}
51 changes: 0 additions & 51 deletions doc/api.md

This file was deleted.

51 changes: 4 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,18 @@
const Lws = require('lws')
const path = require('path')

/**
* @module local-web-server
* @emits module:local-web-server#verbose
* @example
* const LocalWebServer = require('local-web-server')
* const localWebServer = new LocalWebServer()
* const server = localWebServer.listen({
* port: 8050,
* https: true,
* directory: 'src',
* spa: 'index.html',
* websocket: 'src/websocket-server.js'
* })
* // secure, SPA server with listening websocket now ready on port 8050
*/

/**
* @alias module:local-web-server
*/
class LocalWebServer extends Lws {
/**
* Returns a listening HTTP/HTTPS server.
* @param [options] {object} - Server options
* @param [options.port] {number} - Port
* @param [options.hostname] {string} -The hostname (or IP address) to listen on. Defaults to 0.0.0.0.
* @param [options.maxConnections] {number} - The maximum number of concurrent connections supported by the server.
* @param [options.keepAliveTimeout] {number} - The period (in milliseconds) of inactivity a connection will remain open before being destroyed. Set to `0` to keep connections open indefinitely.
* @param [options.configFile] {string} - Config file path, defaults to 'lws.config.js'.
* @param [options.https] {boolean} - Enable HTTPS using a built-in key and cert registered to the domain 127.0.0.1.
* @param [options.key] {string} - SSL key file path. Supply along with --cert to launch a https server.
* @param [options.cert] {string} - SSL cert file path. Supply along with --key to launch a https server.
* @param [options.pfx] {string} - Path to an PFX or PKCS12 encoded private key and certificate chain. An alternative to providing --key and --cert.
* @param [options.ciphers] {string} - Optional cipher suite specification, replacing the default.
* @param [options.secureProtocol] {string} - Optional SSL method to use, default is "SSLv23_method".
* @param [options.stack] {string[]|Middlewares[]} - Array of feature classes, or filenames of modules exporting a feature class.
* @param [options.server] {string|ServerFactory} - Custom server factory, e.g. lws-http2.
* @param [options.websocket] {string|Websocket} - Path to a websocket module
* @param [options.moduleDir] {string[]} - One or more directories to search for modules.
* @returns {Server}
*/
listen (options) {
options = Object.assign({
moduleDir: path.resolve(__dirname, `./node_modules`),
modulePrefix: 'lws-',
_getDefaultConfig () {
return Object.assign(super._getDefaultConfig(), {
moduleDir: [ __dirname, '.' ],
stack: require('./lib/default-stack')
}, options)
return super.listen(options)

/**
* Highly-verbose debug information event stream.
*
* @event module:local-web-server#verbose
* @param key {string} - An identifying string, e.g. `server.socket.data`.
* @param value {*} - The value, e.g. `{ socketId: 1, bytesRead: '3 Kb' }`.
*/
})
}
}

Loading