在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:villadora/express-http-proxy开源软件地址:https://github.com/villadora/express-http-proxy开源编程语言:JavaScript 100.0%开源软件介绍:express-http-proxyExpress middleware to proxy request to another host and pass response back to original caller. Install$ npm install express-http-proxy --save Usageproxy(host, options); Example:To proxy URLS starting with '/proxy' to the host 'www.google.com': var proxy = require('express-http-proxy');
var app = require('express')();
app.use('/proxy', proxy('www.google.com')); StreamingProxy requests and user responses are piped/streamed/chunked by default. If you define a response modifier (userResDecorator, userResHeaderDecorator), or need to inspect the response before continuing (maybeSkipToNext), streaming is disabled, and the request and response are buffered. This can cause performance issues with large payloads. PromisesMany function hooks support Promises.
If any Promise is rejected, e.g. app.use(proxy('/reject-promise', {
proxyReqOptDecorator: function() {
return Promise.reject('An arbitrary rejection message.');
}
})); eventually calls next('An arbitrary rejection messasage'); HostThe first positional argument is for the proxy host; in many cases you will use a static string here, eg. app.use('/', proxy('http://google.com')) However, this argument can also be a function, and that function can be
memoized or computed on each request, based on the setting of
function selectProxyHost() {
return (new Date() % 2) ? 'http://google.com' : 'http://altavista.com';
}
app.use('/', proxy(selectProxyHost)); Middleware mixingIf you use 'https://www.npmjs.com/package/body-parser' you should declare it AFTER the proxy configuration, otherwise original 'POST' body could be modified and not proxied correctly.
OptionsproxyReqPathResolver (supports Promises)Note: In Provide a proxyReqPathResolver function if you'd like to operate on the path before issuing the proxy request. Use a Promise for async operations. app.use(proxy('localhost:12345', {
proxyReqPathResolver: function (req) {
var parts = req.url.split('?');
var queryString = parts[1];
var updatedPath = parts[0].replace(/test/, 'tent');
return updatedPath + (queryString ? '?' + queryString : '');
}
})); Promise form app.use('/proxy', proxy('localhost:12345', {
proxyReqPathResolver: function(req) {
return new Promise(function (resolve, reject) {
setTimeout(function () { // simulate async
var parts = req.url.split('?');
var queryString = parts[1];
var updatedPath = parts[0].replace(/test/, 'tent');
var resolvedPathValue = updatedPath + (queryString ? '?' + queryString : '');
resolve(resolvedPathValue);
}, 200);
});
}
})); forwardPathDEPRECATED. See proxyReqPathResolver forwardPathAsyncDEPRECATED. See proxyReqPathResolver filter (supports Promises)The For example, if you only want to proxy get request: app.use('/proxy', proxy('www.google.com', {
filter: function(req, res) {
return req.method == 'GET';
}
})); Promise form: app.use(proxy('localhost:12346', {
filter: function (req, res) {
return new Promise(function (resolve) {
resolve(req.method === 'GET');
});
}
})); Note that in the previous example, userResDecorator (was: intercept) (supports Promise)You can modify the proxy's response before sending it to the client. app.use('/proxy', proxy('www.google.com', {
userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
data = JSON.parse(proxyResData.toString('utf8'));
data.newProperty = 'exciting data';
return JSON.stringify(data);
}
})); app.use(proxy('httpbin.org', {
userResDecorator: function(proxyRes, proxyResData) {
return new Promise(function(resolve) {
proxyResData.funkyMessage = 'oi io oo ii';
setTimeout(function() {
resolve(proxyResData);
}, 200);
});
}
})); 304 - Not ModifiedWhen your proxied service returns 304, not modified, this step will be skipped, since there is no body to decorate. exploiting referencesThe intent is that this be used to modify the proxy response data only. Note: The other arguments (proxyRes, userReq, userRes) are passed by reference, so you can currently exploit this to modify either response's headers, for instance, but this is not a reliable interface. I expect to close this exploit in a future release, while providing an additional hook for mutating the userRes before sending. gzip responsesIf your proxy response is gzipped, this program will automatically unzip it before passing to your function, then zip it back up before piping it to the user response. There is currently no way to short-circuit this behavior. limitThis sets the body size limit (default: app.use('/proxy', proxy('www.google.com', {
limit: '5mb'
})); memoizeHostDefaults to When true, the When E.g., function coinToss() { return Math.random() > .5 }
function getHost() { return coinToss() ? 'http://yahoo.com' : 'http://google.com' }
app.use(proxy(getHost, {
memoizeHost: false
})) In this example, when Conversely, When userResHeaderDecoratorWhen a app.use('/proxy', proxy('www.google.com', {
userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {
// recieves an Object of headers, returns an Object of headers.
return headers;
}
})); decorateRequestREMOVED: See skipToNextHandlerFilter(supports Promise form)(experimental: this interface may change in upcoming versions) Allows you to inspect the proxy response, and decide if you want to continue processing (via express-http-proxy) or call app.use('/proxy', proxy('www.google.com', {
skipToNextHandlerFilter: function(proxyRes) {
return proxyRes.statusCode === 404;
}
})); proxyErrorHandlerBy default, If you would like to modify this behavior, you can provide your own // Example of skipping all error handling.
app.use(proxy('localhost:12346', {
proxyErrorHandler: function(err, res, next) {
next(err);
}
}));
// Example of rolling your own
app.use(proxy('localhost:12346', {
proxyErrorHandler: function(err, res, next) {
switch (err && err.code) {
case 'ECONNRESET': { return res.status(405).send('504 became 405'); }
case 'ECONNREFUSED': { return res.status(200).send('gotcher back'); }
default: { next(err); }
}
}})); proxyReqOptDecorator (supports Promise form)You can override most request options before issuing the proxyRequest. proxyReqOpt represents the options argument passed to the (http|https).request module. NOTE: req.path cannot be changed via this method; use app.use('/proxy', proxy('www.google.com', {
proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
// you can update headers
proxyReqOpts.headers['Content-Type'] = 'text/html';
// you can change the method
proxyReqOpts.method = 'GET';
return proxyReqOpts;
}
})); You can use a Promise for async style. app.use('/proxy', proxy('www.google.com', {
proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
return new Promise(function(resolve, reject) {
proxyReqOpts.headers['Content-Type'] = 'text/html';
resolve(proxyReqOpts);
})
}
})); proxyReqBodyDecorator (supports Promise form)You can mutate the body content before sending the proxyRequest. app.use('/proxy', proxy('www.google.com', {
proxyReqBodyDecorator: function(bodyContent, srcReq) {
return bodyContent.split('').reverse().join('');
}
})); You can use a Promise for async style. app.use('/proxy', proxy('www.google.com', {
proxyReqBodyDecorator: function(proxyReq, srcReq) {
return new Promise(function(resolve, reject) {
http.get('http://dev/null', function (err, res) {
if (err) { reject(err); }
resolve(res);
});
})
}
})); httpsNormally, your proxy request will be made on the same protocol as the app.use('/proxy', proxy('www.google.com', {
https: true
})); preserveHostHdrYou can copy the host HTTP header to the proxied express server using the app.use('/proxy', proxy('www.google.com', {
preserveHostHdr: true
})); parseReqBodyThe |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论