rewrite and examples.. refactor

This commit is contained in:
Lloyd Brookes
2015-11-16 20:47:34 +00:00
parent 28e1cfdae9
commit 89b427e1fa
7 changed files with 96 additions and 70 deletions

View File

@ -6,7 +6,6 @@ const Koa = require('koa')
const convert = require('koa-convert')
const cors = require('kcors')
const _ = require('koa-route')
const mount = require('koa-mount')
const pathToRegexp = require('path-to-regexp')
/**
@ -43,63 +42,32 @@ function localWebServer (options) {
const _use = app.use
app.use = x => _use.call(app, convert(x))
/* Proxy routes */
if (options.proxyRoutes.length) {
const httpProxy = require('http-proxy')
const proxy = httpProxy.createProxyServer({
changeOrigin: true
})
options.proxyRoutes.forEach(route => {
app.use(_.all(route.from, function * () {
const keys = []
route.re = pathToRegexp(route.from, keys)
route.new = route.to
this.response = false
keys.forEach((key, index) => {
const re = {
token: RegExp('\\$\\{' + key.name + '\\}', 'g'),
index: RegExp('\\$\\{' + index + '\\}', 'g')
}
route.new = route.new
.replace(re.token, arguments[index] || '')
.replace(re.index, arguments[index] || '')
})
proxy.once('proxyReq', function (proxyReq) {
proxyReq.path = url.parse(route.new).path;
})
proxy.web(this.req, this.res, { target: route.new })
}))
})
}
/* Rewrite rules */
if (options.rewrite && options.rewrite.length) {
const rewrite = require('koa-rewrite')
options.rewrite.forEach(rule => {
app.use(rewrite(rule.from, rule.to))
})
}
/* CORS: allow from any origin */
app.use(cors())
/* path blacklist */
if (options.forbid.length) {
app.use(function blacklist (ctx, next) {
if (options.forbid.some(regexp => regexp.test(ctx.path))) {
ctx.throw(403, http.STATUS_CODES[403])
} else {
return next()
/* rewrite rules */
if (options.rewrite && options.rewrite.length) {
options.rewrite.forEach(route => {
if (route.to) {
if (url.parse(route.to).host) {
app.use(_.all(route.from, proxyRequest(route)))
} else {
const rewrite = require('koa-rewrite')
app.use(rewrite(route.from, route.to))
}
}
})
}
/* path blacklist */
if (options.forbid.length) {
app.use(blacklist(options.forbid))
}
/* Cache */
if (!options['no-cache']) {
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
const conditional = require('koa-conditional-get')
const etag = require('koa-etag')
app.use(conditional())
app.use(etag())
}
@ -168,6 +136,54 @@ function logstalgiaDate () {
.replace(' (BST)', '')
}
function proxyRequest (route) {
const httpProxy = require('http-proxy')
const proxy = httpProxy.createProxyServer({
changeOrigin: true
})
return function * proxyMiddleware () {
const next = arguments[arguments.length-1]
const keys = []
route.re = pathToRegexp(route.from, keys)
route.new = this.path.replace(route.re, route.to)
keys.forEach((key, index) => {
const re = RegExp(`:${key.name}`, 'g')
route.new = route.new
.replace(re, arguments[index] || '')
})
/* test no keys remain in the new path */
keys.length = 0
pathToRegexp(route.new, keys)
if (keys.length) {
this.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
yield next
}
this.response = false
proxy.once('error', err => {
this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
})
proxy.once('proxyReq', function (proxyReq) {
proxyReq.path = url.parse(route.new).path;
})
proxy.web(this.req, this.res, { target: route.new })
}
}
function blacklist (forbid) {
return function blacklist (ctx, next) {
if (forbid.some(regexp => regexp.test(ctx.path))) {
ctx.throw(403, http.STATUS_CODES[403])
} else {
return next()
}
}
}
process.on('unhandledRejection', (reason, p) => {
throw reason
})