3 examples of 'http proxy middleware' in JavaScript

Every line of 'http proxy middleware' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
86var middleware = function createProxy(request, response, next) {
87 for (var i = 0; i < proxiesList.length; i++) {
88 if (request.url.indexOf(proxiesList[i]) === 0) {
89 var proxiedUrl = proxies[proxiesList[i]];
90
91 log.debug('proxying request - %s to %s:%s', request.url, proxiedUrl.host, proxiedUrl.port);
92 request.url = request.url.replace(proxiesList[i], proxiedUrl.baseProxyUrl);
93 proxy.proxyRequest(request, response, {
94 host: proxiedUrl.host,
95 port: proxiedUrl.port,
96 target: {https: proxiedUrl.https, rejectUnauthorized: proxyValidateSSL}
97 });
98 return;
99 }
100 }
101
102 return next();
103};
122function proxyMiddleware(req, res, next) {
123 /*
124 * This test is the switch of each request to determine if the request is
125 * for a static file to be handled by BrowserSync or a backend request to proxy.
126 *
127 * The existing test is a standard check on the files extensions but it may fail
128 * for your needs. If you can, you could also check on a context in the url which
129 * may be more reliable but can't be generic.
130 */
131 if (nextTest(preventWhen, req, res) || req.url === "/") {
132 next();
133 } else {
134 proxy.web(req, res);
135 }
136}
43function createProxy(method, pathPattern, target) {
44 const filter = (_, req) => {
45 return method ? req.method.toLowerCase() === method.toLowerCase() : true;
46 };
47 const parsedUrl = url.parse(target);
48 const realTarget = [parsedUrl.protocol, parsedUrl.host].join('//');
49 const targetPath = parsedUrl.path;
50
51 const pathRewrite = (path, req) => {
52 let matchPath = req.originalUrl;
53 const matches = matchPath.match(pathPattern);
54
55 if (matches.length > 1) {
56 matchPath = matches[1];
57 }
58
59 return path.replace(req.originalUrl.replace(matchPath, ''), targetPath);
60 };
61
62 return proxy(filter, { target: realTarget, pathRewrite });
63}

Related snippets