diff --git a/bin/ws.js b/bin/ws.js index 2a83136..bc3fa10 100755 --- a/bin/ws.js +++ b/bin/ws.js @@ -22,8 +22,7 @@ try { options.stored = loadConfig('local-web-server') options.builtIn = { port: 8000, - directory: process.cwd(), - mime: {} + directory: process.cwd() } /* override built-in defaults with stored config and then command line args */ @@ -36,8 +35,9 @@ localWebServer({ static: { root: options.cli.server.directory }, serveIndex: { path: options.cli.server.directory, options: { icons: true } }, log: { format: options.cli.server['log-format'] }, - compress: options.cli.server.compress -}).listen(options.cli.server.port, serverUp) + compress: options.cli.server.compress, + mime: options.stored.mime +}).listen(options.cli.server.port, onServerUp) function halt (message) { console.log(ansi.format(`Error: ${message}`, 'red')) @@ -45,11 +45,10 @@ function halt (message) { process.exit(1) } -function serverUp () { - /* write launch information to stderr (stdout is reserved for web log output) */ - if (path.resolve(options.cli.server.directory) === process.cwd()) { - console.error(ansi.format(`serving at [underline]{http://localhost:${options.cli.server.port}}`)) - } else { - console.error(ansi.format(`serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}}`)) - } +function onServerUp () { + console.error(ansi.format( + path.resolve(options.cli.server.directory) === process.cwd() + ? `serving at [underline]{http://localhost:${options.cli.server.port}}` + : `serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}}` + )) } diff --git a/lib/local-web-server.js b/lib/local-web-server.js index fc17d07..3933263 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -1,4 +1,5 @@ 'use strict' +const path = require('path') const Koa = require('koa') const serve = require('koa-static') const convert = require('koa-convert') @@ -30,9 +31,19 @@ function getApp (options) { const app = new Koa() - // app.use((ctx, next) => { - // return next().then(() => ctx.type = 'text/plain') - // }) + // CORS + + if (options.mime) { + app.use((ctx, next) => { + return next().then(() => { + const reqPathExtension = path.extname(ctx.path).slice(1) + Object.keys(options.mime).forEach(mimeType => { + const extsToOverride = options.mime[mimeType] + if (extsToOverride.indexOf(reqPathExtension) > -1) ctx.type = mimeType + }) + }) + }) + } if (options.compress) { app.use(convert(compress())) diff --git a/package.json b/package.json index 4eb61fd..878c447 100644 --- a/package.json +++ b/package.json @@ -7,15 +7,15 @@ }, "main": "lib/local-web-server.js", "license": "MIT", + "keywords": [ "dev", "server", "web", "tool", "front-end", "development", "cors", "mime", "rest" ], "engines": { - "node": ">=0.10.0" + "node": ">=4.0.0" }, "scripts": { - "lint": "jshint bin/ws.js; echo;", "test": "tape test/*.js" }, "repository": "https://github.com/75lb/local-web-server", - "author": "Lloyd Brookes", + "author": "Lloyd Brookes <75pound@gmail.com>", "dependencies": { "command-line-args": "^2.0.2", "compression": "^1.0.2", @@ -44,15 +44,7 @@ "stream-log-stats": "^1", "typical": "^2.0.0" }, - "local-web-server": { - "mime": { - "text/plain": [ - "php" - ] - } - }, "devDependencies": { - "collect-all": "^0.1.0", "tape": "^4.2.2" } } diff --git a/test/.local-web-server.json b/test/static/.local-web-server.json similarity index 100% rename from test/.local-web-server.json rename to test/static/.local-web-server.json diff --git a/test/something.php b/test/static/something.php similarity index 100% rename from test/something.php rename to test/static/something.php diff --git a/test/test.js b/test/test.js index 23916fb..31ad10d 100644 --- a/test/test.js +++ b/test/test.js @@ -5,6 +5,17 @@ const localWebServer = require('../') const http = require('http') const PassThrough = require('stream').PassThrough +function launchServer (app, reqOptions, path, onSuccess) { + path = `http://localhost:8100${path || '/'}` + const server = http.createServer(app.callback()) + server.listen(8100, () => { + const req = request(path, reqOptions) + if (onSuccess) req.then(onSuccess) + req.then(() => server.close()) + req.catch(err => console.error('LAUNCH ERROR', err.stack)) + }) +} + test('log: common', function (t) { t.plan(1) const stream = PassThrough() @@ -70,13 +81,14 @@ test('compress', function(t){ }) }) -function launchServer (app, reqOptions, path, onSuccess) { - path = `http://localhost:8100${path || '/'}` - const server = http.createServer(app.callback()) - server.listen(8100, () => { - const req = request(path, reqOptions) - if (onSuccess) req.then(onSuccess) - req.then(() => server.close()) - req.catch(err => console.error('LAUNCH ERROR', err.stack)) +test('mime', function(t){ + t.plan(1) + const app = localWebServer({ + log: { format: 'none' }, + static: { root: __dirname + '/static' }, + mime: { 'text/plain': [ 'php' ]} }) -} + launchServer(app, null, '/something.php', response => { + t.ok(/text\/plain/.test(response.res.headers['content-type'])) + }) +})