Socket
Socket
Sign inDemoInstall

http-proxy-middleware

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-proxy-middleware

The one-liner node.js proxy middleware for connect, express and browser-sync


Version published
Weekly downloads
14M
increased by5.72%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 13 Jun 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc