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