diff --git a/README.md b/README.md
index b6b5a8f..77fc8fa 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ A simple web-server for productive front-end development.
**Requires node v4.0.0 or higher**.
## Synopsis
-For the examples below, and we assume we're in a project directory looking like this:
+For the examples below, we assume we're in a project directory looking like this:
```sh
.
@@ -83,7 +83,7 @@ $ ws --rewrite '/npm/* -> http://registry.npmjs.org/$1'
Map local requests for repo data to the Github API:
```sh
-$ ws --rewrite '/projects/:user/repos/:name -> https://api.github.com/repos/:user/:name'
+$ ws --rewrite '/:user/repos/:name -> https://api.github.com/repos/:user/:name'
```
### Stored config
@@ -100,7 +100,7 @@ Use the same port and blacklist every time? Persist it to `package.json`:
}
```
-.. or `.local-web-server.json`
+or `.local-web-server.json`
```json
{
"port": 8100,
@@ -210,11 +210,15 @@ serving at http://localhost:8100
Returns a Koa application
**Kind**: Exported function
-
-| Param | Type | Description |
-| --- | --- | --- |
-| [options] | object
| options |
-| [options.forbid] | Array.<regexp>
| a list of forbidden routes. |
+**Params**
+- [options] object
- options
+ - [.static] object
- koajs/static config
+ - [.root] string
- root directory
+ - [.options] string
- options
+ - [.serveIndex] object
- koa-serve-index config
+ - [.path] string
- root directory
+ - [.options] string
- options
+ - [.forbid] Array.<string>
- a list of forbidden routes.
**Example**
```js
diff --git a/example/rewrite/.local-web-server.json b/example/rewrite/.local-web-server.json
index 1d05efd..ae46592 100644
--- a/example/rewrite/.local-web-server.json
+++ b/example/rewrite/.local-web-server.json
@@ -2,6 +2,6 @@
"rewrite": [
{ "from": "/css/*", "to": "/build/styles/$1" },
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
- { "from": "/projects/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
+ { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
]
}
diff --git a/example/rewrite/index.html b/example/rewrite/index.html
index 5705c2c..2711101 100644
--- a/example/rewrite/index.html
+++ b/example/rewrite/index.html
@@ -7,9 +7,9 @@
{
"rewrite": [
- { "from": "/css/*", "to": "/styles/$1" },
+ { "from": "/css/*", "to": "/build/styles/$1" },
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
- { "from": "/gh/:user/repo/:name", "to": "https://api.github.com/repos/:user/:name" }
+ { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
]
}
@@ -18,5 +18,5 @@
diff --git a/jsdoc2md/README.hbs b/jsdoc2md/README.hbs
index c08da76..8de83d8 100644
--- a/jsdoc2md/README.hbs
+++ b/jsdoc2md/README.hbs
@@ -10,7 +10,7 @@ A simple web-server for productive front-end development.
**Requires node v4.0.0 or higher**.
## Synopsis
-For the examples below, and we assume we're in a project directory looking like this:
+For the examples below, we assume we're in a project directory looking like this:
```sh
.
@@ -83,7 +83,7 @@ $ ws --rewrite '/npm/* -> http://registry.npmjs.org/$1'
Map local requests for repo data to the Github API:
```sh
-$ ws --rewrite '/projects/:user/repos/:name -> https://api.github.com/repos/:user/:name'
+$ ws --rewrite '/:user/repos/:name -> https://api.github.com/repos/:user/:name'
```
### Stored config
@@ -100,7 +100,7 @@ Use the same port and blacklist every time? Persist it to `package.json`:
}
```
-.. or `.local-web-server.json`
+or `.local-web-server.json`
```json
{
"port": 8100,
diff --git a/lib/local-web-server.js b/lib/local-web-server.js
index 9ffdf3d..db94de2 100644
--- a/lib/local-web-server.js
+++ b/lib/local-web-server.js
@@ -17,7 +17,14 @@ module.exports = localWebServer
* Returns a Koa application
*
* @param [options] {object} - options
- * @param [options.forbid] {regexp[]} - a list of forbidden routes.
+ * @param [options.static] {object} - koajs/static config
+ * @param [options.static.root] {string} - root directory
+ * @param [options.static.options] {string} - options
+ * @param [options.serveIndex] {object} - koa-serve-index config
+ * @param [options.serveIndex.path] {string} - root directory
+ * @param [options.serveIndex.options] {string} - options
+ * @param [options.forbid] {string[]} - a list of forbidden routes.
+ *
* @alias module:local-web-server
* @example
* const localWebServer = require('local-web-server')
@@ -29,6 +36,7 @@ function localWebServer (options) {
serveIndex: {},
log: {},
compress: false,
+ mime: {},
forbid: [],
rewrite: []
}, options)
@@ -138,35 +146,35 @@ function proxyRequest (route) {
changeOrigin: true
})
- return function * proxyMiddleware () {
- const next = arguments[arguments.length-1]
+ return function proxyMiddleware (ctx) {
+ const next = arguments[arguments.length - 1]
const keys = []
route.re = pathToRegexp(route.from, keys)
- route.new = this.path.replace(route.re, route.to)
+ route.new = ctx.path.replace(route.re, route.to)
keys.forEach((key, index) => {
const re = RegExp(`:${key.name}`, 'g')
route.new = route.new
- .replace(re, arguments[index] || '')
+ .replace(re, arguments[index + 1] || '')
})
/* test no keys remain in the new path */
keys.length = 0
pathToRegexp(route.new, keys)
if (keys.length) {
- this.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
- yield next
+ ctx.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
+ return next()
}
- this.response = false
+ ctx.response = false
proxy.once('error', err => {
- this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
+ ctx.throw(500, `[PROXY] ${err.message}: ${route.new}`)
})
proxy.once('proxyReq', function (proxyReq) {
proxyReq.path = url.parse(route.new).path;
})
- proxy.web(this.req, this.res, { target: route.new })
+ proxy.web(ctx.req, ctx.res, { target: route.new })
}
}
diff --git a/package.json b/package.json
index 066da16..2de092d 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,7 @@
},
"scripts": {
"test": "tape test/*.js",
- "docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo",
+ "docs": "jsdoc2md -t jsdoc2md/README.hbs -p list lib/*.js > README.md; echo",
"cover": "istanbul cover ./node_modules/.bin/tape test/*.js && cat coverage/lcov.info | coveralls && rm -rf coverage; echo"
},
"repository": "https://github.com/75lb/local-web-server",
diff --git a/test/fixture/forbid/one.html b/test/fixture/forbid/one.html
new file mode 100644
index 0000000..5626abf
--- /dev/null
+++ b/test/fixture/forbid/one.html
@@ -0,0 +1 @@
+one
diff --git a/test/fixture/forbid/two.php b/test/fixture/forbid/two.php
new file mode 100644
index 0000000..abb2fca
--- /dev/null
+++ b/test/fixture/forbid/two.php
@@ -0,0 +1 @@
+
diff --git a/test/fixture/rewrite/one.html b/test/fixture/rewrite/one.html
new file mode 100644
index 0000000..5626abf
--- /dev/null
+++ b/test/fixture/rewrite/one.html
@@ -0,0 +1 @@
+one
diff --git a/test/fixture/something.php b/test/fixture/something.php
new file mode 100644
index 0000000..abb2fca
--- /dev/null
+++ b/test/fixture/something.php
@@ -0,0 +1 @@
+
diff --git a/test/test.js b/test/test.js
index 43d5d6f..51d767f 100644
--- a/test/test.js
+++ b/test/test.js
@@ -17,26 +17,6 @@ function launchServer (app, options) {
})
}
-test('log: common', function (t) {
- t.plan(1)
- const stream = PassThrough()
-
- stream.on('readable', () => {
- let chunk = stream.read()
- if (chunk) t.ok(/GET/.test(chunk.toString()))
- })
-
- const app = localWebServer({
- log: {
- format: 'common',
- options: {
- stream: stream
- }
- }
- })
- launchServer(app)
-})
-
test('static', function (t) {
t.plan(1)
const app = localWebServer({
@@ -70,6 +50,26 @@ test('serve-index', function (t) {
}})
})
+test('log: common', function (t) {
+ t.plan(1)
+ const stream = PassThrough()
+
+ stream.on('readable', () => {
+ let chunk = stream.read()
+ if (chunk) t.ok(/GET/.test(chunk.toString()))
+ })
+
+ const app = localWebServer({
+ log: {
+ format: 'common',
+ options: {
+ stream: stream
+ }
+ }
+ })
+ launchServer(app)
+})
+
test('compress', function(t){
t.plan(1)
const app = localWebServer({
@@ -90,13 +90,14 @@ test('compress', function(t){
})
test('mime', function(t){
- t.plan(1)
+ t.plan(2)
const app = localWebServer({
log: { format: 'none' },
static: { root: __dirname + '/fixture' },
mime: { 'text/plain': [ 'php' ]}
})
launchServer(app, { path: '/something.php', onSuccess: response => {
+ t.strictEqual(response.res.statusCode, 200)
t.ok(/text\/plain/.test(response.res.headers['content-type']))
}})
})
@@ -105,14 +106,14 @@ test('forbid', function (t) {
t.plan(2)
const app = localWebServer({
log: { format: 'none' },
- static: { root: __dirname + '/fixture' },
- forbid: [ /php$/, /html$/ ]
+ static: { root: __dirname + '/fixture/forbid' },
+ forbid: [ '*.php', '*.html' ]
})
const server = launchServer(app, { leaveOpen: true })
- request('http://localhost:8100/something.php')
+ request('http://localhost:8100/two.php')
.then(response => {
t.strictEqual(response.res.statusCode, 403)
- request('http://localhost:8100/ajax.html')
+ request('http://localhost:8100/one.html')
.then(response => {
t.strictEqual(response.res.statusCode, 403)
server.close()
@@ -120,26 +121,27 @@ test('forbid', function (t) {
})
})
-test.skip('directories: should serve index and static files', function(t){
+test('rewrite: local', function(t){
t.plan(1)
const app = localWebServer({
log: { format: 'none' },
- directories: [
- __dirname + '/fixture/one'
- ]
+ static: { root: __dirname + '/fixture/rewrite' },
+ rewrite: [ { from: '/two.html', to: '/one.html'} ]
})
- launchServer(app, { path: '/something.php', onSuccess: response => {
- t.ok(/text\/plain/.test(response.res.headers['content-type']))
+ launchServer(app, { path: '/two.html', onSuccess: response => {
+ t.strictEqual(response.data, 'one\n')
}})
})
-test('proxy', function(t){
- t.plan(1)
+test('rewrite: proxy', function(t){
+ t.plan(2)
const app = localWebServer({
log: { format: 'none' },
- proxy: []
+ static: { root: __dirname + '/fixture/rewrite' },
+ rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1'} ]
})
- launchServer(app, { path: '/something.php', onSuccess: response => {
- t.ok(/text\/plain/.test(response.res.headers['content-type']))
+ launchServer(app, { path: '/test/', onSuccess: response => {
+ t.strictEqual(response.res.statusCode, 200)
+ t.ok(/db_name/.test(response.data))
}})
})