proxy-middleware: no more a middleware
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
exports = module.exports = {
|
||||
cookieParser: require('cookie-parser'),
|
||||
cors: require('./cors.js'),
|
||||
proxy: require('./proxy-middleware.js'),
|
||||
lastMile: require('connect-lastmile'),
|
||||
multipart: require('./multipart.js'),
|
||||
timeout: require('connect-timeout')
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
// https://github.com/cloudron-io/node-proxy-middleware
|
||||
// MIT license
|
||||
// contains https://github.com/gonzalocasas/node-proxy-middleware/pull/59
|
||||
|
||||
const { request } = require('http');
|
||||
|
||||
function slashJoin(p1, p2) {
|
||||
var trailing_slash = false;
|
||||
|
||||
if (p1.length && p1[p1.length - 1] === '/') { trailing_slash = true; }
|
||||
if (trailing_slash && p2.length && p2[0] === '/') {p2 = p2.substring(1); }
|
||||
|
||||
return p1 + p2;
|
||||
}
|
||||
|
||||
module.exports = function proxyMiddleware(targetUrl) {
|
||||
const options = require('url').parse(targetUrl);
|
||||
|
||||
return function (req, resp, next) {
|
||||
const { url } = req;
|
||||
const opts = Object.assign({}, options);
|
||||
if (url && url.charAt(0) === '?') { // prevent /api/resource/?offset=0
|
||||
if (options.pathname.length > 1 && options.pathname.charAt(options.pathname.length - 1) === '/') {
|
||||
opts.path = options.pathname.substring(0, options.pathname.length - 1) + url;
|
||||
} else {
|
||||
opts.path = options.pathname + url;
|
||||
}
|
||||
} else if (url) {
|
||||
opts.path = slashJoin(options.pathname, url);
|
||||
} else {
|
||||
opts.path = options.pathname;
|
||||
}
|
||||
opts.method = req.method;
|
||||
opts.headers = Object.assign({}, req.headers);
|
||||
delete opts.headers.host;
|
||||
|
||||
var myReq = request(opts, function (myRes) {
|
||||
resp.writeHead(myRes.statusCode, myRes.headers);
|
||||
myRes.on('error', function (err) {
|
||||
next(err);
|
||||
});
|
||||
myRes.on('end', function (err) {
|
||||
next();
|
||||
});
|
||||
myRes.pipe(resp);
|
||||
});
|
||||
myReq.on('error', function (err) {
|
||||
next(err);
|
||||
});
|
||||
if (!req.readable) {
|
||||
myReq.end();
|
||||
} else {
|
||||
req.pipe(myReq);
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user