koa2-nginx
Advanced tools
Comparing version 2.0.0 to 2.0.1
const httpProxyMiddleware = require('http-proxy-middleware'); | ||
const c2k = require('koa2-connect'); | ||
const qs = require('querystring'); | ||
/** | ||
* Middleware for delegate koa request/response to req.koaReq and res.koaRes | ||
* | ||
* @param {*} ctx | ||
* @param {*} next | ||
*/ | ||
async function delegateKoaClass2StreamMid(ctx, next) { | ||
// delegate koa request/response to nodejs req.koaReq and res.koaRes | ||
ctx.req.koaReq = ctx.request; | ||
ctx.res.koaRes = ctx.response; | ||
await next(); | ||
} | ||
/** | ||
* Transform http-proxy-middleware to koa's middleware | ||
* @param {*} proxyConfig | ||
* @returns | ||
*/ | ||
function getProxyMiddleware(proxyConfig) { | ||
@@ -12,2 +31,33 @@ const context = proxyConfig.context || proxyConfig.path || '/'; | ||
/** | ||
* When koa2-nginx is behind the body-parser middleware, | ||
* solve the problem that the body cannot be proxyed | ||
* @param {*} originProxyReq | ||
* @returns proxyReq event | ||
*/ | ||
function wraperProxyReqHandler(onProxyReq) { | ||
return function(proxyReq, req, res, options) { | ||
if (typeof onProxyReq === 'function') { | ||
onProxyReq(proxyReq, req, res, options); | ||
} | ||
if (req.koaReq.body) { | ||
const contentType = proxyReq.getHeader('Content-Type'); | ||
let bodyData; | ||
if (contentType.match(/application\/json/)) { | ||
bodyData = JSON.stringify(req.koaReq.body); | ||
} else if (contentType.match(/application\/x-www-form-urlencoded/)) { | ||
bodyData = qs.stringify(req.koaReq.body); | ||
} | ||
if (bodyData) { | ||
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData)); | ||
proxyReq.write(bodyData); | ||
proxyReq.end(); | ||
} | ||
} | ||
}; | ||
} | ||
exports.wraperProxyReqHandler = wraperProxyReqHandler; | ||
exports.setupProxyFeature = function(options) { | ||
@@ -42,3 +92,3 @@ if (!Array.isArray(options)) { | ||
exports.setupMiddlewares = function(options) { | ||
const proxyMiddlewares = []; | ||
const proxyMiddlewares = [delegateKoaClass2StreamMid]; | ||
@@ -51,2 +101,6 @@ options.forEach(proxyConfigOrCallback => { | ||
if (proxyConfig.autoProcessReqBody && proxyConfig.onProxyReq) { | ||
proxyConfig.onProxyReq = wraperProxyReqHandler(proxyConfig.onProxyReq); | ||
} | ||
const proxyMiddleware = getProxyMiddleware(proxyConfig); | ||
@@ -53,0 +107,0 @@ |
{ | ||
"name": "koa2-nginx", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "This is an http-proxy koa proxy middleware that can be used after bodyparse", | ||
@@ -22,5 +22,7 @@ "main": "index.js", | ||
"koa-compose": "^4.1.0", | ||
"koa2-connect": "^1.0.2" | ||
"koa2-connect": "^1.0.2", | ||
"querystring": "^0.2.0" | ||
}, | ||
"devDependencies": { | ||
"codecov": "^3.6.1", | ||
"cz-conventional-changelog": "^3.0.2", | ||
@@ -27,0 +29,0 @@ "eslint": "^6.6.0", |
# koa2-nginx | ||
![npm](https://img.shields.io/npm/v/koa2-nginx) | ||
[![Coverage Status](https://coveralls.io/repos/github/my9074/koa2-nginx/badge.svg?branch=master)](https://coveralls.io/github/my9074/koa2-nginx?branch=master) | ||
[![codecov](https://codecov.io/gh/my9074/koa2-nginx/branch/master/graph/badge.svg)](https://codecov.io/gh/my9074/koa2-nginx) | ||
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) | ||
@@ -46,2 +46,3 @@ [![dependency Status](https://img.shields.io/david/my9074/koa2-nginx.svg?style=flat-square)](https://david-dm.org/my9074/koa2-nginx#info=dependencies) | ||
- [Working examples](#working-examples) | ||
- [FAQ](#faq) | ||
@@ -89,4 +90,14 @@ <!-- /MarkdownTOC --> | ||
### koa2-nginx options | ||
- **autoProcessReqBody**: If **koa2-nginx** is behind the **body-parser**, it may cause the request body to fail to proxy. Set `autoProcessReqBody` to **true** can proxy the request body in `json` and `form` content-type. | ||
### http-proxy-middleware options | ||
Can refer option to [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware#options) for each forwarding rule. | ||
### http-proxy options | ||
Can refer option to [http-proxy](https://github.com/http-party/node-http-proxy#options) for each forwarding rule. | ||
## Usage | ||
@@ -140,2 +151,5 @@ | ||
- [EggJS]() TODO | ||
- [EggJS]() TODO | ||
## FAQ | ||
1. POST/PUT request body is not proxied to the servers [#40](https://github.com/chimurai/http-proxy-middleware/issues/40#issuecomment-163398924) or set `autoProcessReqBody` to `true` |
const expect = require('expect.js'); | ||
const { setupProxyFeature, setupMiddlewares } = require('../lib'); | ||
const { | ||
setupProxyFeature, | ||
setupMiddlewares, | ||
wraperProxyReqHandler | ||
} = require('../lib'); | ||
@@ -72,5 +76,61 @@ describe('utils', function() { | ||
const middles = setupMiddlewares(opt); | ||
expect(middles).to.have.length(1); | ||
expect(middles).to.have.length(1 + 1); | ||
}); | ||
it('option has autoProcessReqBody field', function() { | ||
const opt = [ | ||
{ | ||
context: '/api', | ||
target: 'http://www.example.com', | ||
autoProcessReqBody: true, | ||
onProxyReq(proxyRes, req, res) { | ||
// test | ||
} | ||
} | ||
]; | ||
setupMiddlewares(opt); | ||
}); | ||
}); | ||
describe('wraperProxyReqHandler', function() { | ||
it('when context-type is application/json', function() { | ||
const onProxyReq = function(proxyReq, req, res, options) {}; | ||
const arg1 = { | ||
getHeader: args => { | ||
return 'application/json'; | ||
}, | ||
setHeader: (key, value) => {}, | ||
write: args => {}, | ||
end: () => {} | ||
}; | ||
const arg2 = { | ||
koaReq: { | ||
body: { a: 1 } | ||
} | ||
}; | ||
const arg3 = {}; | ||
wraperProxyReqHandler(onProxyReq)(arg1, arg2, arg3); | ||
}); | ||
it('when context-type is application/x-www-form-urlencoded', function() { | ||
const onProxyReq = function(proxyReq, req, res, options) {}; | ||
const arg1 = { | ||
getHeader: args => { | ||
return 'application/x-www-form-urlencoded'; | ||
}, | ||
setHeader: (key, value) => {}, | ||
write: args => {}, | ||
end: () => {} | ||
}; | ||
const arg2 = { | ||
koaReq: { | ||
body: 'a=1' | ||
} | ||
}; | ||
const arg3 = {}; | ||
wraperProxyReqHandler(onProxyReq)(arg1, arg2, arg3); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14439
245
152
1
4
14
+ Addedquerystring@^0.2.0
+ Addedquerystring@0.2.1(transitive)