🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

http-proxy-middleware

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
h

http-proxy-middleware

The one-liner node.js proxy middleware for connect, express, next.js and more

3.0.5
latest
99

Supply Chain Security

100

Vulnerability

100

Quality

83

Maintenance

100

License

Network access

Supply chain risk

This module accesses the network.

Found 1 instance in 1 package

Version published
Weekly downloads
22M
-1.03%
Maintainers
1
Weekly downloads
 
Created
Issues
114

What is http-proxy-middleware?

The http-proxy-middleware package is a Node.js package that provides an HTTP proxy as a middleware for use with Node.js applications, particularly in conjunction with frameworks like Express. It allows developers to easily set up proxy rules to forward requests to other servers, which is useful for tasks like API forwarding, logging, handling CORS, and more.

What are http-proxy-middleware's main functionalities?

Proxy requests

This feature allows you to proxy requests to another server. In this example, all requests to '/api' on the local server are forwarded to 'http://www.example.org'.

const { createProxyMiddleware } = require('http-proxy-middleware');

const apiProxy = createProxyMiddleware('/api', { target: 'http://www.example.org' });

app.use('/api', apiProxy);

Path Rewriting

This feature allows you to rewrite the path of the request URL before it gets proxied. In this example, the path '/api' is removed before the request is forwarded to 'http://www.example.org'.

const { createProxyMiddleware } = require('http-proxy-middleware');

const apiProxy = createProxyMiddleware('/api', {
  target: 'http://www.example.org',
  pathRewrite: { '^/api': '' }
});

app.use('/api', apiProxy);

Custom Routing Logic

This feature allows you to implement custom routing logic. In this example, only GET requests to paths starting with '/api' are proxied to 'http://www.example.org'.

const { createProxyMiddleware } = require('http-proxy-middleware');

const apiProxy = createProxyMiddleware((pathname, req) => {
  return pathname.match('^/api') && req.method === 'GET';
}, { target: 'http://www.example.org' });

app.use(apiProxy);

Handling WebSockets

This feature allows you to proxy WebSocket connections. In this example, WebSocket connections to '/socket' are proxied to 'ws://www.example.org'.

const { createProxyMiddleware } = require('http-proxy-middleware');

const wsProxy = createProxyMiddleware('/socket', {
  target: 'ws://www.example.org',
  ws: true
});

app.use('/socket', wsProxy);

Other packages similar to http-proxy-middleware

FAQs

Package last updated on 10 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts