diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..d007aa8 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +repo_token: w9HmlMl9558e1LpP9p62YgYutkVE9PqtN diff --git a/README.md b/README.md index 2739ff1..9c5cd4b 100644 --- a/README.md +++ b/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 ``` -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 @@ -54,8 +54,6 @@ When urls don't map to your directory structure, rewrite: $ ws --rewrite /css=>/build/css ``` -### Proxy - Rewrite to remote servers (proxy): ```sh $ ws --rewrite "/api => http://api.example.com/api" \ diff --git a/bin/cli.js b/bin/cli.js index 21ca30f..22e0c92 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,11 +1,9 @@ #!/usr/bin/env node 'use strict' -const s = Date.now() - const localWebServer = require('../') +const cliOptions = require('../lib/cli-options') const commandLineArgs = require('command-line-args') const ansi = require('ansi-escape-sequences') -const cliOptions = require('../lib/cli-options') const loadConfig = require('config-master') const path = require('path') @@ -37,14 +35,15 @@ localWebServer({ hidden: true } }, - log: { format: options.server['log-format'] }, + log: { + format: options.server['log-format'] + }, compress: options.server.compress, mime: options.server.mime, forbid: options.server.forbid.map(regexp => RegExp(regexp, 'i')), - proxyRoutes: options.server.proxyRoutes, spa: options.server.spa, 'no-cache': options.server['no-cache'], - rewrite: options.server.rewrite + rewrite: parseRewriteRules(options.server.rewrite) }).listen(options.server.port, onServerUp) function halt (err) { @@ -54,12 +53,10 @@ function halt (err) { } function onServerUp () { - const e = Date.now() - const time = `${e - s}ms` console.error(ansi.format( 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) return options } + +function parseRewriteRules (rules) { + return rules.map(rule => { + const matches = rule.match(/(\S*)\s*->\s*(\S*)/) + return { + from: matches[1], + to: matches[2] + } + }) +} diff --git a/example/mime-override/.local-web-server.json b/example/mime-override/.local-web-server.json new file mode 100644 index 0000000..d569d29 --- /dev/null +++ b/example/mime-override/.local-web-server.json @@ -0,0 +1,5 @@ +{ + "mime": { + "text/plain": [ "php" ] + } +} diff --git a/test/fixture/something.php b/example/mime-override/something.php similarity index 100% rename from test/fixture/something.php rename to example/mime-override/something.php diff --git a/jsdoc2md/README.hbs b/jsdoc2md/README.hbs index 71fdcf0..a84fa81 100644 --- a/jsdoc2md/README.hbs +++ b/jsdoc2md/README.hbs @@ -54,8 +54,6 @@ When urls don't map to your directory structure, rewrite: $ ws --rewrite /css=>/build/css ``` -### Proxy to external services - Rewrite to remote servers (proxy): ```sh $ ws --rewrite "/api => http://api.example.com/api" \ diff --git a/lib/cli-options.js b/lib/cli-options.js index 13e3775..ca0b02e 100644 --- a/lib/cli-options.js +++ b/lib/cli-options.js @@ -6,7 +6,7 @@ module.exports = { }, { 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, @@ -34,15 +34,15 @@ module.exports = { }, { 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: { 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}', synopsis: [ - '$ ws ', + '$ ws [server options]', '$ ws --config', '$ ws --help' ], diff --git a/lib/local-web-server.js b/lib/local-web-server.js index f23f70d..e419ea1 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -30,8 +30,6 @@ function localWebServer (options) { log: {}, compress: false, forbid: [], - directories: [], - proxyRoutes: [], rewrite: [] }, options) diff --git a/package.json b/package.json index 9b0c339..f95e58b 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ }, "scripts": { "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", "author": "Lloyd Brookes <75pound@gmail.com>", diff --git a/test/fixture/.local-web-server.json b/test/fixture/.local-web-server.json deleted file mode 100644 index 8858020..0000000 --- a/test/fixture/.local-web-server.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "mime": { - "text/plain": [ "php" ] - }, - "forbid": [ - "php$" - ] -}