Browse Source

implement mime-type overrides.. tests.

master
Lloyd Brookes 9 years ago
parent
commit
69db357661
  1. 21
      bin/ws.js
  2. 17
      lib/local-web-server.js
  3. 14
      package.json
  4. 0
      test/static/.local-web-server.json
  5. 0
      test/static/something.php
  6. 30
      test/test.js

21
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}}`
))
}

17
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()))

14
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"
}
}

0
test/.local-web-server.json → test/static/.local-web-server.json

0
test/something.php → test/static/something.php

30
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']))
})
})
Loading…
Cancel
Save