Skip to content

Commit 0f2ebbe

Browse files
authored
Merge pull request #7 from vagusX/dev
support express-styles route
2 parents 5d5abb6 + 1659b66 commit 0f2ebbe

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

examples/server.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@ const proxyTable = {
1212
'/octocat': 'https://api.github.com/users',
1313
'/api': {
1414
target: 'http://localhost:8111',
15+
logs: true,
1516
headers: {
1617
'X_HOST_S': 'google.com'
1718
}
1819
},
19-
'/users': {
20+
'/tenant/:id': params => {
21+
return {
22+
target: 'http://localhost:8111',
23+
logs: true,
24+
headers: {
25+
'X_HOST_S': 'google.com'
26+
}
27+
}
28+
},
29+
'/users/:id': {
2030
target: 'https://api.github.com',
2131
changeOrigin: true,
2232
logs: true,
23-
agent: proxy ? new HttpsProxyAgent(agentUrl) : null,
33+
agent: agentUrl ? new HttpsProxyAgent(agentUrl) : null,
2434
headers: {
2535
'XHostS': 'google.com'
2636
},
@@ -38,12 +48,11 @@ const proxyTable = {
3848

3949
const app = new Koa()
4050

41-
app.use((ctx, next) => {
42-
return next()
43-
.then(() => {
44-
ctx.request.headers['X-Request-Header'] = 'test'
45-
ctx.response.headers['X-Response-Header'] = 'test'
46-
})
51+
app.use(async (ctx, next) => {
52+
// console.log('-----------------')
53+
// console.log(ctx.request.headers)
54+
// console.log('-----------------')
55+
await next()
4756
})
4857

4958
Object.keys(proxyTable).forEach(context => {

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,45 @@
33
*/
44

55
const HttpProxy = require('http-proxy')
6+
const pathMatch = require('path-match')
67

78
/**
89
* Constants
910
*/
1011

1112
const proxy = HttpProxy.createProxyServer()
13+
const route = pathMatch({
14+
// path-to-regexp options
15+
sensitive: false,
16+
strict: false,
17+
end: false
18+
})
1219

1320
/**
1421
* Koa Http Proxy Middleware
1522
*/
16-
1723
module.exports = (context, options) => (ctx, next) => {
18-
if (!ctx.req.url.startsWith(context)) return next()
24+
// create a match function
25+
const match = route(context)
26+
if (!match(ctx.req.url)) return next()
1927

2028
let opts = options
2129
if (typeof options === 'function') {
22-
opts = options.call(options)
30+
const params = match(ctx.req.url)
31+
opts = options.call(options, params)
2332
}
2433

2534
const { logs, rewrite, events } = opts
2635

2736
return new Promise((resolve, reject) => {
28-
if (logs) logger(ctx)
37+
ctx.req.oldPath = ctx.req.url
2938

3039
if (typeof rewrite === 'function') {
3140
ctx.req.url = rewrite(ctx.req.url)
3241
}
3342

43+
if (logs) logger(ctx)
44+
3445
if (events && typeof events === 'object') {
3546
Object.entries(events).forEach(([event, handler]) => {
3647
proxy.on(event, handler)
@@ -53,5 +64,5 @@ module.exports = (context, options) => (ctx, next) => {
5364
}
5465

5566
function logger (ctx) {
56-
console.log('%s - %s %s', new Date().toISOString(), ctx.req.method, ctx.req.url)
67+
console.log('%s - %s %s proxy to -> %s', new Date().toISOString(), ctx.req.method, ctx.req.oldPath, ctx.req.url)
5768
}

package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"validate-commit-message": "^3.0.1"
3333
},
3434
"dependencies": {
35-
"http-proxy": "^1.16.2"
35+
"http-proxy": "^1.16.2",
36+
"path-match": "^1.2.4"
3637
}
3738
}

0 commit comments

Comments
 (0)