Lloyd Brookes
9 years ago
24 changed files with 18 additions and 614 deletions
-
7lib/local-web-server.js
-
2package.json
-
195test/compress/big-file.txt
-
22test/compress/compress.js
-
21test/forbid/forbid.js
-
1test/forbid/one.html
-
1test/forbid/two.php
-
25test/log/log.js
-
22test/mime/mime.js
-
1test/mime/something.php
-
150test/mock/mock.js
-
1test/mock/one.html
-
1test/proxy/file.txt
-
1test/proxy/one.html
-
73test/proxy/rewrite-proxy.js
-
1test/rewrite/one.html
-
19test/rewrite/rewrite.js
-
18test/serve-index/serve-index.js
-
1test/spa/one.txt
-
28test/spa/spa.js
-
1test/spa/two.txt
-
1test/static/file.txt
-
18test/static/static.js
-
16test/test.js
@ -1,195 +0,0 @@ |
|||
[![view on npm](http://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server) |
|||
[![npm module downloads per month](http://img.shields.io/npm/dm/local-web-server.svg)](https://www.npmjs.org/package/local-web-server) |
|||
[![Dependency Status](https://david-dm.org/75lb/local-web-server.svg)](https://david-dm.org/75lb/local-web-server) |
|||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) |
|||
|
|||
# local-web-server |
|||
Fires up a simple, CORS-enabled, static web server on a given port. Use for local web development or file sharing (directory browsing enabled). |
|||
|
|||
![local-web-server](http://75lb.github.io/local-web-server/ws.gif) |
|||
|
|||
## Install |
|||
Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`. |
|||
|
|||
### Globally |
|||
```sh |
|||
$ npm install -g local-web-server |
|||
``` |
|||
|
|||
### Bundled with your project |
|||
```sh |
|||
$ npm install local-web-server --save-dev |
|||
``` |
|||
|
|||
Then add an `start` script to your `package.json` (the standard npm approach): |
|||
```json |
|||
{ |
|||
"name": "my-web-app", |
|||
"version": "1.0.0", |
|||
"scripts": { |
|||
"start": "ws" |
|||
} |
|||
} |
|||
``` |
|||
This simplifies a rather specific-looking instruction set like: |
|||
|
|||
```sh |
|||
$ npm install |
|||
$ npm install -g local-web-server |
|||
$ ws |
|||
``` |
|||
|
|||
to the following, server implementation and launch details abstracted away: |
|||
```sh |
|||
$ npm install |
|||
$ npm start |
|||
``` |
|||
|
|||
## Usage |
|||
``` |
|||
Usage |
|||
$ ws <server options> |
|||
$ ws --config |
|||
$ ws --help |
|||
|
|||
Server |
|||
-p, --port <number> Web server port |
|||
-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 |
|||
preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> |
|||
:url'). |
|||
-d, --directory <string> Root directory, defaults to the current directory |
|||
-c, --compress Enable gzip compression, reduces bandwidth. |
|||
-r, --refresh-rate <number> Statistics view refresh rate in ms. Defaults to 500. |
|||
|
|||
Misc |
|||
-h, --help Print these usage instructions |
|||
--config Print the stored config |
|||
``` |
|||
|
|||
From the folder you wish to serve, run: |
|||
```sh |
|||
$ ws |
|||
serving at http://localhost:8000 |
|||
``` |
|||
|
|||
If you wish to serve a different directory, run: |
|||
```sh |
|||
$ ws -d ~/mysite/ |
|||
serving /Users/Lloyd/mysite at http://localhost:8000 |
|||
``` |
|||
|
|||
If you wish to override the default port (8000), use `--port` or `-p`: |
|||
```sh |
|||
$ ws --port 9000 |
|||
serving at http://localhost:9000 |
|||
``` |
|||
|
|||
To add compression, reducing bandwidth, increasing page load time (by 10-15% on my Macbook Air) |
|||
```sh |
|||
$ ws --compress |
|||
``` |
|||
|
|||
### Logging |
|||
Passing a value to `--log.format` will write an access log to `stdout`. |
|||
|
|||
Either use a built-in [morgan](https://github.com/expressjs/morgan) logger preset: |
|||
```sh |
|||
$ ws --log.format short |
|||
``` |
|||
|
|||
Or a custom [morgan](https://github.com/expressjs/morgan) log format: |
|||
```sh |
|||
$ ws -f ':method -> :url' |
|||
``` |
|||
|
|||
Or silence: |
|||
```sh |
|||
$ ws -f none |
|||
``` |
|||
|
|||
## Storing default options |
|||
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`: |
|||
```json |
|||
{ |
|||
"name": "my-project", |
|||
"version": "0.11.8", |
|||
"local-web-server":{ |
|||
"port": 8100 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site): |
|||
```json |
|||
{ |
|||
"port": 8100, |
|||
"log.format": "tiny" |
|||
} |
|||
``` |
|||
|
|||
Or store global defaults in a `.local-web-server.json` file in your home directory. |
|||
```json |
|||
{ |
|||
"port": 3000, |
|||
"refresh-rate": 1000 |
|||
} |
|||
``` |
|||
|
|||
All stored defaults are overriden by options supplied at the command line. |
|||
|
|||
To view your stored defaults, run: |
|||
|
|||
```sh |
|||
$ ws --config |
|||
``` |
|||
|
|||
## mime-types |
|||
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: |
|||
|
|||
```json |
|||
{ |
|||
"mime": { |
|||
"text/plain": [ "php", "pl" ] |
|||
} |
|||
} |
|||
``` |
|||
|
|||
## Use with Logstalgia |
|||
local-web-server is compatible with [logstalgia](http://code.google.com/p/logstalgia/). |
|||
|
|||
### Install Logstalgia |
|||
On MacOSX, install with [homebrew](http://brew.sh): |
|||
```sh |
|||
$ brew install logstalgia |
|||
``` |
|||
|
|||
Alternatively, [download a release for your system from github](https://github.com/acaudwell/Logstalgia/releases/latest). |
|||
|
|||
Then pipe the `logstalgia` output format directly into logstalgia for real-time visualisation: |
|||
```sh |
|||
$ ws -f logstalgia | logstalgia - |
|||
``` |
|||
|
|||
![local-web-server with logstalgia](http://75lb.github.io/local-web-server/logstagia.gif) |
|||
|
|||
## Use with glTail |
|||
To use with [glTail](http://www.fudgie.org), write your log to disk using the "default" format: |
|||
```sh |
|||
$ ws -f default > web.log |
|||
``` |
|||
|
|||
Then specify this file in your glTail config: |
|||
|
|||
```yaml |
|||
servers: |
|||
dev: |
|||
host: localhost |
|||
source: local |
|||
files: /Users/Lloyd/Documents/MySite/web.log |
|||
parser: apache |
|||
color: 0.2, 0.2, 1.0, 1.0 |
|||
``` |
|||
|
|||
* * * |
|||
|
|||
© 2015 Lloyd Brookes <75pound@gmail.com> |
@ -1,22 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('compress', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addCompression(true) |
|||
ws.addStatic(__dirname) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/big-file.txt', { headers: { 'Accept-Encoding': 'gzip' } }) |
|||
.then(response => { |
|||
t.strictEqual(response.res.statusCode, 200) |
|||
t.strictEqual(response.res.headers['content-encoding'], 'gzip') |
|||
}) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
@ -1,21 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('forbid', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addBlacklist([ '*.php', '*.html' ]) |
|||
ws.addStatic(__dirname) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/two.php') |
|||
.then(c.checkResponse(t, 403)) |
|||
.then(() => request('http://localhost:8100/one.html')) |
|||
.then(c.checkResponse(t, 403)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
@ -1 +0,0 @@ |
|||
one |
@ -1 +0,0 @@ |
|||
<?php echo "i'm coding PHP templatez!\n" ?>
|
@ -1,25 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('logging', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
|
|||
const stream = require('stream').PassThrough() |
|||
stream.on('readable', () => { |
|||
let chunk = stream.read() |
|||
if (chunk) t.ok(/GET/.test(chunk.toString())) |
|||
}) |
|||
|
|||
ws.addLogging('common', { stream: stream }) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/') |
|||
.then(c.checkResponse(t, 404)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
@ -1,22 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('mime override', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addMimeOverride({ 'text/plain': [ 'php' ] }) |
|||
ws.addStatic(__dirname) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/something.php') |
|||
.then(response => { |
|||
t.strictEqual(response.res.statusCode, 200) |
|||
t.ok(/text\/plain/.test(response.res.headers['content-type'])) |
|||
}) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
@ -1 +0,0 @@ |
|||
<?php echo "i'm coding PHP templatez!\n" ?>
|
@ -1,150 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('mock: simple response', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addMockResponses([ |
|||
{ route: '/test', response: { body: 'test' } } |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test') |
|||
.then(c.checkResponse(t, 200, /test/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('mock: method request filter', function (t) { |
|||
t.plan(3) |
|||
const ws = new LocalWebServer() |
|||
ws.addMockResponses([ |
|||
{ |
|||
route: '/test', |
|||
request: { method: 'POST' }, |
|||
response: { body: 'test' } |
|||
} |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test') |
|||
.then(c.checkResponse(t, 404)) |
|||
.then(() => request('http://localhost:8100/test', { data: 'something' })) |
|||
.then(c.checkResponse(t, 200, /test/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('mock: accepts request filter', function (t) { |
|||
t.plan(3) |
|||
const ws = new LocalWebServer() |
|||
ws.addMockResponses([ |
|||
{ |
|||
route: '/test', |
|||
request: { accepts: 'text' }, |
|||
response: { body: 'test' } |
|||
} |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test', { headers: { Accept: '*/json' } }) |
|||
.then(c.checkResponse(t, 404)) |
|||
.then(() => request('http://localhost:8100/test', { headers: { Accept: 'text/plain' } })) |
|||
.then(c.checkResponse(t, 200, /test/)) |
|||
.then(server.close.bind(server)) |
|||
}) |
|||
}) |
|||
|
|||
test('mock: responses array', function (t) { |
|||
t.plan(4) |
|||
const ws = new LocalWebServer() |
|||
ws.addMockResponses([ |
|||
{ |
|||
route: '/test', |
|||
responses: [ |
|||
{ request: { method: 'GET' }, response: { body: 'get' } }, |
|||
{ request: { method: 'POST' }, response: { body: 'post' } } |
|||
] |
|||
} |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test') |
|||
.then(c.checkResponse(t, 200, /get/)) |
|||
.then(() => request('http://localhost:8100/test', { method: 'POST' })) |
|||
.then(c.checkResponse(t, 200, /post/)) |
|||
.then(server.close.bind(server)) |
|||
}) |
|||
}) |
|||
|
|||
test('mock: response function', function (t) { |
|||
t.plan(4) |
|||
const ws = new LocalWebServer() |
|||
ws.addMockResponses([ |
|||
{ |
|||
route: '/test', |
|||
responses: [ |
|||
{ request: { method: 'GET' }, response: ctx => ctx.body = 'get' }, |
|||
{ request: { method: 'POST' }, response: ctx => ctx.body = 'post' } |
|||
] |
|||
} |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test') |
|||
.then(c.checkResponse(t, 200, /get/)) |
|||
.then(() => request('http://localhost:8100/test', { method: 'POST' })) |
|||
.then(c.checkResponse(t, 200, /post/)) |
|||
.then(server.close.bind(server)) |
|||
}) |
|||
}) |
|||
|
|||
test('mock: response function args', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addMockResponses([ |
|||
{ |
|||
route: '/test/:one', |
|||
responses: [ |
|||
{ request: { method: 'GET' }, response: (ctx, one) => ctx.body = one } |
|||
] |
|||
} |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test/yeah') |
|||
.then(c.checkResponse(t, 200, /yeah/)) |
|||
.then(server.close.bind(server)) |
|||
}) |
|||
}) |
|||
|
|||
test('mock: async response function', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addMockResponses([ |
|||
{ |
|||
route: '/test', |
|||
responses: { |
|||
response: function (ctx) { |
|||
return new Promise((resolve, reject) => { |
|||
setTimeout(() => { |
|||
ctx.body = 'test' |
|||
resolve() |
|||
}, 10) |
|||
}) |
|||
} |
|||
} |
|||
} |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test') |
|||
.then(c.checkResponse(t, 200, /test/)) |
|||
.then(server.close.bind(server)) |
|||
}) |
|||
}) |
@ -1 +0,0 @@ |
|||
one |
@ -1 +0,0 @@ |
|||
one |
@ -1 +0,0 @@ |
|||
one |
@ -1,73 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const http = require('http') |
|||
const c = require('../common') |
|||
|
|||
test('rewrite: proxy', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addRewrite([ |
|||
{ from: '/test/*', to: 'http://registry.npmjs.org/$1' } |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test/') |
|||
.then(c.checkResponse(t, 200, /db_name/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('rewrite: proxy, POST', function (t) { |
|||
t.plan(1) |
|||
const ws = new LocalWebServer() |
|||
ws.addRewrite([ |
|||
{ from: '/test/*', to: 'http://registry.npmjs.org/' } |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test/', { data: {} }) |
|||
.then(c.checkResponse(t, 405)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('rewrite: proxy, two url tokens', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addRewrite([ |
|||
{ from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/command-line-args/1.0.0') |
|||
.then(c.checkResponse(t, 200, /command-line-args/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('rewrite: proxy with port', function (t) { |
|||
t.plan(2) |
|||
const ws1 = new LocalWebServer() |
|||
ws1.addStatic(__dirname) |
|||
|
|||
const ws2 = new LocalWebServer() |
|||
ws2.addRewrite([ |
|||
{ from: '/test/*', to: 'http://localhost:9000/$1' } |
|||
]) |
|||
const server1 = ws1.getServer() |
|||
const server2 = ws2.getServer() |
|||
server1.listen(9000, () => { |
|||
server2.listen(8100, () => { |
|||
request('http://localhost:8100/test/file.txt') |
|||
.then(c.checkResponse(t, 200, /one/)) |
|||
.then(server1.close.bind(server1)) |
|||
.then(server2.close.bind(server2)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
|||
}) |
@ -1 +0,0 @@ |
|||
one |
@ -1,19 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('rewrite local', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addRewrite([ { from: '/two.html', to: '/one.html' } ]) |
|||
ws.addStatic(__dirname) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/two.html') |
|||
.then(c.checkResponse(t, 200, /one/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
@ -1,18 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('static', function (t) { |
|||
t.plan(3) |
|||
const ws = new LocalWebServer() |
|||
ws.addIndex(__dirname, { icons: true }) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/') |
|||
.then(c.checkResponse(t, 200, [ /listing directory/, /class="icon/ ])) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
@ -1 +0,0 @@ |
|||
one |
@ -1,28 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('spa', function (t) { |
|||
t.plan(6) |
|||
const ws = new LocalWebServer() |
|||
ws.addSpa('one.txt') |
|||
ws.addStatic(__dirname) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/asdf', { headers: { accept: 'text/html' } }) |
|||
.then(c.checkResponse(t, 200, /one/)) |
|||
/* html requests for missing files with extensions do not redirect to spa */ |
|||
.then(() => request('http://localhost:8100/asdf.txt', { headers: { accept: 'text/html' } })) |
|||
.then(c.checkResponse(t, 404)) |
|||
/* existing static file */ |
|||
.then(() => request('http://localhost:8100/two.txt')) |
|||
.then(c.checkResponse(t, 200, /two/)) |
|||
/* not a text/html request - does not redirect to spa */ |
|||
.then(() => request('http://localhost:8100/asdf', { headers: { accept: 'application/json' } })) |
|||
.then(c.checkResponse(t, 404)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
@ -1 +0,0 @@ |
|||
two |
@ -1 +0,0 @@ |
|||
test |
@ -1,18 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const c = require('../common') |
|||
|
|||
test('static', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addStatic(__dirname, { index: 'file.txt' }) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/') |
|||
.then(c.checkResponse(t, 200, /test/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(c.fail(t)) |
|||
}) |
|||
}) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue