Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
http-proxy-middleware
Advanced tools
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.
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);
express-http-proxy is similar to http-proxy-middleware but is specifically designed for use with Express. It offers similar features for proxying HTTP requests but may have different configuration options and middleware setup.
node-http-proxy is a full-featured HTTP proxy library for Node.js, which http-proxy-middleware is built upon. It provides more low-level control over proxying but requires more setup compared to the convenience middleware layer provided by http-proxy-middleware.
Redbird is a reverse proxy library for Node.js with built-in support for clustering, HTTP2, LetsEncrypt, and more. It is more feature-rich and suitable for more complex proxying needs compared to http-proxy-middleware, which is simpler and more focused on middleware use cases.
Middleware for connect and browser-sync
npm install --save-dev http-proxy-middleware
Create and configure the proxy middleware so it can be used as middleware in connect or browser-sync.
var proxyMiddleware = require('http-proxy-middleware');
var proxy = proxyMiddleware(context, options);
context
path to proxy. Example: '/api'options.target
target host to proxy to. (See "Options" for all options)Example: Proxy http://localhost:3000/ajax requests to http://cdnjs.cloudfare.com/ajax
var http = require('http');
var connect = require('connect');
var proxyMiddleware = require('http-proxy-middleware');
var context = '/ajax';
var proxy = proxyMiddleware(context, {target: 'http://cdnjs.cloudflare.com'});
var app = connect();
app.use(context, proxy);
http.createServer(app).listen(3000);
Example: Proxy http://localhost:3000/ajax requests to http://cdnjs.cloudfare.com/ajax
var browserSync = require('browser-sync');
var proxyMiddleware = require('http-proxy-middleware');
var proxy = proxyMiddleware('/ajax', {target: 'http://cdnjs.cloudflare.com'});
browserSync({
server: {
baseDir: "./",
port: 3000,
middleware: [proxy]
}
});
Example: Proxy http://localhost:3000/ajax requests to http://cdnjs.cloudfare.com/ajax
var gulp = require('gulp');
var browserSync = require('browser-sync');
var proxyMiddleware = require('http-proxy-middleware');
gulp.task('serve', function () {
var proxy = proxyMiddleware('/ajax', {target: 'http://cdnjs.cloudflare.com'});
browserSync({
server: {
baseDir: "./",
port: 3000,
middleware: [proxy]
}
});
});
gulp.task('default', ['serve']);
path
(useful for proxying to proxies)The MIT License (MIT)
Copyright (c) 2015 Steven Chim
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
The one-liner node.js proxy middleware for connect, express, next.js and more
The npm package http-proxy-middleware receives a total of 11,771,656 weekly downloads. As such, http-proxy-middleware popularity was classified as popular.
We found that http-proxy-middleware demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.