What is proxy-middleware?
The proxy-middleware npm package is a simple middleware for connecting to a proxy server. It is typically used in development environments to forward requests to another server, often to avoid cross-origin issues or to simulate a production environment.
What are proxy-middleware's main functionalities?
Basic Proxy Setup
This code sets up a basic proxy server that forwards requests from '/api' to 'http://example.com'. It uses the 'connect' package to create a server and the 'proxy-middleware' package to handle the proxying.
const connect = require('connect');
const http = require('http');
const proxy = require('proxy-middleware');
const url = require('url');
const app = connect();
const proxyOptions = url.parse('http://example.com');
proxyOptions.route = '/api';
app.use(proxy(proxyOptions));
http.createServer(app).listen(3000);
Custom Headers
This code demonstrates how to add custom headers to the proxied requests. The 'headers' option in 'proxyOptions' allows you to specify any headers you want to include in the request.
const connect = require('connect');
const http = require('http');
const proxy = require('proxy-middleware');
const url = require('url');
const app = connect();
const proxyOptions = url.parse('http://example.com');
proxyOptions.route = '/api';
proxyOptions.headers = { 'X-Special-Proxy-Header': 'foobar' };
app.use(proxy(proxyOptions));
http.createServer(app).listen(3000);
Path Rewriting
This code shows how to rewrite the path of the proxied request. The 'rewrite' option in 'proxyOptions' allows you to specify a mapping of paths to be rewritten.
const connect = require('connect');
const http = require('http');
const proxy = require('proxy-middleware');
const url = require('url');
const app = connect();
const proxyOptions = url.parse('http://example.com');
proxyOptions.route = '/api';
proxyOptions.rewrite = { '^/api': '/new-api' };
app.use(proxy(proxyOptions));
http.createServer(app).listen(3000);
Other packages similar to proxy-middleware
http-proxy-middleware
The 'http-proxy-middleware' package is a more feature-rich alternative to 'proxy-middleware'. It provides additional functionalities such as context matching, path rewriting, and more advanced proxying options. It is also actively maintained and widely used in the community.
node-http-proxy
The 'node-http-proxy' package is a powerful HTTP proxy library for Node.js. It offers a wide range of features including WebSocket proxying, load balancing, and more. It is more complex than 'proxy-middleware' but provides greater flexibility and control over proxying behavior.
express-http-proxy
The 'express-http-proxy' package is designed specifically for use with Express.js. It provides a simple way to proxy requests in an Express application, with support for custom headers, path rewriting, and more. It is a good choice if you are already using Express.js in your project.
Usage:
var connect = require('connect');
var url = require('url');
var proxy = require('proxy-middleware');
var app = connect();
app.use('/api', proxy(url.parse('https://example.com/endpoint')));
app.use('/api-string-only', proxy('https://example.com/endpoint'));
Documentation:
proxyMiddleware(options)
options
allows any options that are permitted on the http
or https
request options.
Other options:
route
: you can pass the route for connect middleware within the options, as well.via
: by default no via header is added. If you pass true
for this option the local hostname will be used for the via header. You can also pass a string for this option in which case that will be used for the via header.cookieRewrite
: this option can be used to support cookies via the proxy by rewriting the cookie domain to that of the proxy server. By default cookie domains are not rewritten. The cookieRewrite
option works as the via
option - if you pass true
the local hostname will be used, and if you pass a string that will be used as the rewritten cookie domain.preserveHost
: When enabled, this option will pass the Host: line from the incoming request to the proxied host. Default: false
.
Usage with route:
var proxyOptions = url.parse('https://example.com/endpoint');
proxyOptions.route = '/api';
var middleWares = [proxy(proxyOptions) ];
connect(middleWares);