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

proxy-middleware

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

proxy-middleware

http(s) proxy as connect middleware

  • 0.8.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
182K
decreased by-57.42%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 04 Dec 2014

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