How to use the createProxyServer function from http-proxy

Find comprehensive JavaScript http-proxy.createProxyServer code examples handpicked from public code repositorys.

http-proxy.createProxyServer is a function in Node.js that creates a proxy server that forwards HTTP requests to a target server.

104
105
106
107
108
109
110
111
112
113
  contentType: this.contentType,
  handleError: typeof options.proxy !== 'string'
}))

if (typeof options.proxy === 'string') {
  var proxy = nm_httpProxy.createProxyServer({})
  before.push(function (req, res) {
    proxy.web(req, res, {
      target: options.proxy,
      changeOrigin: true
fork icon2
star icon11
watch icon11

+ 4 other calls in file

169
170
171
172
173
174
175
176
177
178
179
180
}


/**
 * 创建代理服务器,执行动态代理
 */
const proxyServer = httpProxy.createProxyServer({
  secure: false, ws: true, changeOrigin: false, hostRewrite: true, timeout: 20 * 1000, proxyTimeout: 20 * 1000,
});


proxyServer.on('proxyRes', (proxyRes, req) => {
fork icon0
star icon1
watch icon0

How does http-proxy.createProxyServer work?

http-proxy.createProxyServer is a function provided by the http-proxy module in Node.js that creates a new proxy server. This function returns an instance of a proxy server that can be used to intercept HTTP requests and forward them to a target server. The createProxyServer function takes an optional options object as an argument that can be used to configure the behavior of the proxy server. Some of the options that can be specified include the target server that the proxy should forward requests to, the SSL configuration for the proxy server, and the headers that should be sent along with forwarded requests. Once a proxy server has been created using createProxyServer, it can be used to intercept incoming HTTP requests by listening to the request event. The proxy server can then be configured to forward the request to the target server using the proxy.web method. The createProxyServer function also supports several other methods that can be used to configure the behavior of the proxy server, such as proxy.on and proxy.once, which can be used to attach event listeners to the proxy server, and proxy.close, which can be used to shut down the proxy server.

0
1
2
3
4
5
6
7
8
9
10
const url = require('url');
const async = require('async');
const httpProxy = require('http-proxy');
const querystring = require('querystring');


const backbeatProxy = httpProxy.createProxyServer({
    ignorePath: true,
});
const { auth, errors, s3middleware, s3routes, models, storage } =
    require('arsenal');
fork icon237
star icon0
watch icon89

+ 4 other calls in file

1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
});
httpServer.on('error', function (error) {
    errorProcessedRequests ++;
    cannotListen(error);
});
proxyServer = new httpProxy.createProxyServer(proxyServerOptions);
proxyServer.on('error', proxyErrorHandler);
proxyServer.on('proxyReq', proxyRequestRewrite);
proxyServer.on('proxyRes', proxyResponseRewrite);
proxyServer.on('end', proxyResponseComplete);
fork icon4
star icon10
watch icon4

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const http = require("http");
const httpProxy = require("http-proxy");

const proxy = httpProxy.createProxyServer({ target: "http://localhost:3000" });

const server = http.createServer((req, res) => {
  proxy.web(req, res);
});

server.listen(8080);
console.log("Proxy server listening on port 8080");

In this example, we create a new HTTP server using the built-in http module in Node.js. We then create a new instance of the proxy server using http-proxy.createProxyServer and configure it to forward requests to a target server running on http://localhost:3000. We then attach an event listener to the HTTP server for the request event. When a request is received, we pass it to the proxy.web method of the proxy server, which will forward the request to the target server specified in the options. Finally, we start the HTTP server and listen for incoming requests on port 8080. Note that this is just a basic example, and there are many more configuration options and features available when using http-proxy.createProxyServer.

40
41
42
43
44
45
46
47
48
    this._proxy = null
  }
}

_createProxy () {
  this._proxy = httpProxy.createProxyServer({
    target: this._settings.target
  })
}
fork icon0
star icon2
watch icon6

+ 4 other calls in file

135
136
137
138
139
140
141
142
143
144
    this.logger.error(errorMessage, requestHref, targetHref, err.code || err, errReference);
};
this.config = config_factory_1.createConfig(context, opts);
this.proxyOptions = this.config.options;
// create proxy
this.proxy = httpProxy.createProxyServer({});
this.logger.info(`[HPM] Proxy created: ${this.config.context}  -> ${this.proxyOptions.target}`);
this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided
// attach handler to http-proxy events
handlers.init(this.proxy, this.proxyOptions);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

33
34
35
36
37
38
39
40
41
42
43
readStream.on('end', () => {
    readStream.close();
});


// Upgrade proxy
const wsProxy = new httpProxy.createProxyServer({
	target: {
		host: 'localhost',
		port: PORT,
	},
fork icon0
star icon0
watch icon1

6
7
8
9
10
11
12
13
14
15
var execSync = require('child_process').execSync;
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var httpProxy = require('http-proxy');
var cors = require('cors')
var proxy = httpProxy.createProxyServer({})
var ejs = require('ejs');
var fileupload = require("express-fileupload");
module.exports = function(s,config,lang,app,io){
    const {
fork icon0
star icon0
watch icon1

+ 3 other calls in file

15
16
17
18
19
20
21
22
23
var wsInitialized       = false;
var config              = configFactory.createConfig(context, opts);
var proxyOptions        = config.options;

// create proxy
var proxy = httpProxy.createProxyServer({});
logger.info('[HPM] Proxy created:', config.context, ' -> ', proxyOptions.target);

var pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided
fork icon0
star icon0
watch icon1

+ 4 other calls in file