docs.. examples.. tests
This commit is contained in:
20
README.md
20
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] | <code>object</code> | options |
|
||||
| [options.forbid] | <code>Array.<regexp></code> | a list of forbidden routes. |
|
||||
**Params**
|
||||
- [options] <code>object</code> - options
|
||||
- [.static] <code>object</code> - koajs/static config
|
||||
- [.root] <code>string</code> - root directory
|
||||
- [.options] <code>string</code> - options
|
||||
- [.serveIndex] <code>object</code> - koa-serve-index config
|
||||
- [.path] <code>string</code> - root directory
|
||||
- [.options] <code>string</code> - options
|
||||
- [.forbid] <code>Array.<string></code> - a list of forbidden routes.
|
||||
|
||||
**Example**
|
||||
```js
|
||||
|
@ -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" }
|
||||
]
|
||||
}
|
||||
|
@ -7,9 +7,9 @@
|
||||
<pre><code>
|
||||
{
|
||||
"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" }
|
||||
]
|
||||
}
|
||||
</code></pre>
|
||||
@ -18,5 +18,5 @@
|
||||
<ul>
|
||||
<li><a href="/css/style.css">/css/style.css</li>
|
||||
<li><a href="/npm/local-web-server">/npm/local-web-server</a></li>
|
||||
<li><a href="/gh/75lb/repo/work">/gh/75lb/repo/work</a></li>
|
||||
<li><a href="/75lb/repos/work">/75lb/repos/work</a></li>
|
||||
</ul>
|
||||
|
@ -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,
|
||||
|
@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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",
|
||||
|
1
test/fixture/forbid/one.html
Normal file
1
test/fixture/forbid/one.html
Normal file
@ -0,0 +1 @@
|
||||
one
|
1
test/fixture/forbid/two.php
Normal file
1
test/fixture/forbid/two.php
Normal file
@ -0,0 +1 @@
|
||||
<?php echo "i'm coding PHP templatez!\n" ?>
|
1
test/fixture/rewrite/one.html
Normal file
1
test/fixture/rewrite/one.html
Normal file
@ -0,0 +1 @@
|
||||
one
|
1
test/fixture/something.php
Normal file
1
test/fixture/something.php
Normal file
@ -0,0 +1 @@
|
||||
<?php echo "i'm coding PHP templatez!\n" ?>
|
74
test/test.js
74
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))
|
||||
}})
|
||||
})
|
||||
|
Reference in New Issue
Block a user