Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http-proxy

Package Overview
Dependencies
Maintainers
4
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-proxy

HTTP proxying for the masses

  • 1.18.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16M
increased by11.66%
Maintainers
4
Weekly downloads
 
Created

What is http-proxy?

The http-proxy npm package is a full-featured HTTP proxy library for Node.js. It supports websockets and can be used to proxy different kinds of HTTP and HTTPS traffic, which makes it suitable for implementing reverse proxies and load balancers.

What are http-proxy's main functionalities?

Proxying HTTP/HTTPS requests

This code creates an HTTP server that listens on port 8000 and proxies all incoming requests to 'http://mytarget.com:8080'.

const http = require('http');
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer({});

const server = http.createServer(function(req, res) {
  proxy.web(req, res, { target: 'http://mytarget.com:8080' });
});

server.listen(8000);

Proxying WebSockets

This code sets up a server that can proxy WebSocket connections to 'ws://mytarget.com:8080' when an 'upgrade' event is emitted.

const http = require('http');
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer({});
const server = http.createServer(function(req, res) {
  // This function is not used when proxying WebSockets
});

server.on('upgrade', function(req, socket, head) {
  proxy.ws(req, socket, head, { target: 'ws://mytarget.com:8080' });
});

server.listen(8000);

Listening for proxy events

This code listens for errors and responses from the proxy server, allowing for custom error handling and logging.

const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});

proxy.on('error', function(err, req, res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  });
  res.end('Something went wrong.');
});

proxy.on('proxyRes', function(proxyRes, req, res) {
  console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});

Other packages similar to http-proxy

FAQs

Package last updated on 17 May 2020

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