Browse Source

docs.. tidy.. examples.. --rewrite

master
Lloyd Brookes 9 years ago
parent
commit
7a991cc5f7
  1. 1
      .coveralls.yml
  2. 6
      README.md
  3. 27
      bin/cli.js
  4. 5
      example/mime-override/.local-web-server.json
  5. 0
      example/mime-override/something.php
  6. 2
      jsdoc2md/README.hbs
  7. 8
      lib/cli-options.js
  8. 2
      lib/local-web-server.js
  9. 3
      package.json
  10. 8
      test/fixture/.local-web-server.json

1
.coveralls.yml

@ -0,0 +1 @@
repo_token: w9HmlMl9558e1LpP9p62YgYutkVE9PqtN

6
README.md

@ -35,9 +35,9 @@ You're building a web app with client-side routing, so mark `index.html` as the
$ ws --spa index.html $ ws --spa index.html
``` ```
By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found` as there is no file at that location on disk. By marking `index.html` as the SPA you create this rule:
By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
*if a static file at the requested path exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then serve the SPA.*
*If a static file at the requested path exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then serve the SPA for client-side processing.*
### Access Control ### Access Control
@ -54,8 +54,6 @@ When urls don't map to your directory structure, rewrite:
$ ws --rewrite /css=>/build/css $ ws --rewrite /css=>/build/css
``` ```
### Proxy
Rewrite to remote servers (proxy): Rewrite to remote servers (proxy):
```sh ```sh
$ ws --rewrite "/api => http://api.example.com/api" \ $ ws --rewrite "/api => http://api.example.com/api" \

27
bin/cli.js

@ -1,11 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict' 'use strict'
const s = Date.now()
const localWebServer = require('../') const localWebServer = require('../')
const cliOptions = require('../lib/cli-options')
const commandLineArgs = require('command-line-args') const commandLineArgs = require('command-line-args')
const ansi = require('ansi-escape-sequences') const ansi = require('ansi-escape-sequences')
const cliOptions = require('../lib/cli-options')
const loadConfig = require('config-master') const loadConfig = require('config-master')
const path = require('path') const path = require('path')
@ -37,14 +35,15 @@ localWebServer({
hidden: true hidden: true
} }
}, },
log: { format: options.server['log-format'] },
log: {
format: options.server['log-format']
},
compress: options.server.compress, compress: options.server.compress,
mime: options.server.mime, mime: options.server.mime,
forbid: options.server.forbid.map(regexp => RegExp(regexp, 'i')), forbid: options.server.forbid.map(regexp => RegExp(regexp, 'i')),
proxyRoutes: options.server.proxyRoutes,
spa: options.server.spa, spa: options.server.spa,
'no-cache': options.server['no-cache'], 'no-cache': options.server['no-cache'],
rewrite: options.server.rewrite
rewrite: parseRewriteRules(options.server.rewrite)
}).listen(options.server.port, onServerUp) }).listen(options.server.port, onServerUp)
function halt (err) { function halt (err) {
@ -54,12 +53,10 @@ function halt (err) {
} }
function onServerUp () { function onServerUp () {
const e = Date.now()
const time = `${e - s}ms`
console.error(ansi.format( console.error(ansi.format(
path.resolve(options.server.directory) === process.cwd() path.resolve(options.server.directory) === process.cwd()
? `serving at [underline]{http://localhost:${options.server.port}} ${time}`
: `serving [underline]{${options.server.directory}} at [underline]{http://localhost:${options.server.port}} ${time}`
? `serving at [underline]{http://localhost:${options.server.port}}`
: `serving [underline]{${options.server.directory}} at [underline]{http://localhost:${options.server.port}}`
)) ))
} }
@ -84,3 +81,13 @@ function collectOptions () {
options.server = Object.assign(builtIn, stored, options.server) options.server = Object.assign(builtIn, stored, options.server)
return options return options
} }
function parseRewriteRules (rules) {
return rules.map(rule => {
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
return {
from: matches[1],
to: matches[2]
}
})
}

5
example/mime-override/.local-web-server.json

@ -0,0 +1,5 @@
{
"mime": {
"text/plain": [ "php" ]
}
}

0
test/fixture/something.php → example/mime-override/something.php

2
jsdoc2md/README.hbs

@ -54,8 +54,6 @@ When urls don't map to your directory structure, rewrite:
$ ws --rewrite /css=>/build/css $ ws --rewrite /css=>/build/css
``` ```
### Proxy to external services
Rewrite to remote servers (proxy): Rewrite to remote servers (proxy):
```sh ```sh
$ ws --rewrite "/api => http://api.example.com/api" \ $ ws --rewrite "/api => http://api.example.com/api" \

8
lib/cli-options.js

@ -6,7 +6,7 @@ module.exports = {
}, },
{ {
name: 'log-format', alias: 'f', type: String, name: 'log-format', alias: 'f', type: String,
description: "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').", group: 'server'
description: "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').", group: 'server'
}, },
{ {
name: 'directory', alias: 'd', type: String, name: 'directory', alias: 'd', type: String,
@ -34,15 +34,15 @@ module.exports = {
}, },
{ {
name: 'config', type: Boolean, name: 'config', type: Boolean,
description: 'Print the stored config', group: 'misc'
description: 'Print the config found in [underline]{package.json} and/or [underline]{.local-web-server}', group: 'misc'
} }
], ],
usageData: { usageData: {
title: 'local-web-server', title: 'local-web-server',
description: 'Lightweight static web server, zero configuration.',
description: 'A simple web-server for productive front-end development.',
footer: 'Project home: [underline]{https://github.com/75lb/local-web-server}', footer: 'Project home: [underline]{https://github.com/75lb/local-web-server}',
synopsis: [ synopsis: [
'$ ws <server options>',
'$ ws [server options]',
'$ ws --config', '$ ws --config',
'$ ws --help' '$ ws --help'
], ],

2
lib/local-web-server.js

@ -30,8 +30,6 @@ function localWebServer (options) {
log: {}, log: {},
compress: false, compress: false,
forbid: [], forbid: [],
directories: [],
proxyRoutes: [],
rewrite: [] rewrite: []
}, options) }, options)

3
package.json

@ -23,7 +23,8 @@
}, },
"scripts": { "scripts": {
"test": "tape test/*.js", "test": "tape test/*.js",
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo"
"docs": "jsdoc2md -t jsdoc2md/README.hbs 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", "repository": "https://github.com/75lb/local-web-server",
"author": "Lloyd Brookes <75pound@gmail.com>", "author": "Lloyd Brookes <75pound@gmail.com>",

8
test/fixture/.local-web-server.json

@ -1,8 +0,0 @@
{
"mime": {
"text/plain": [ "php" ]
},
"forbid": [
"php$"
]
}
Loading…
Cancel
Save