
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
express-http-proxy
Advanced tools
The express-http-proxy package is a middleware for Express.js that allows you to easily proxy HTTP requests to another server. It is useful for creating APIs that aggregate data from multiple sources, handling cross-origin requests, and more.
Basic Proxying
This feature allows you to proxy requests from your Express server to another server. In this example, any request to '/proxy' will be forwarded to 'http://example.com'.
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();
app.use('/proxy', proxy('http://example.com'));
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Customizing Proxy Requests
This feature allows you to customize the path of the proxied request. In this example, any request to '/proxy' will be forwarded to 'http://example.com/api' with the original request URL appended.
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();
app.use('/proxy', proxy('http://example.com', {
proxyReqPathResolver: function(req) {
return '/api' + req.url;
}
}));
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Handling Proxy Responses
This feature allows you to modify the response from the proxied server before sending it back to the client. In this example, the word 'example' in the response body is replaced with 'sample'.
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();
app.use('/proxy', proxy('http://example.com', {
userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
return proxyResData.toString('utf8').replace('example', 'sample');
}
}));
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
http-proxy-middleware is a popular middleware for creating proxies in Express.js. It offers more advanced features like path rewriting, logging, and WebSocket support. Compared to express-http-proxy, it provides more flexibility and is more actively maintained.
node-http-proxy is a low-level HTTP proxy library for Node.js. It provides a lot of customization options and can be used to create complex proxy servers. While it is more powerful, it requires more setup and configuration compared to express-http-proxy.
express-proxy is another middleware for proxying HTTP requests in Express.js. It is simpler and less feature-rich compared to express-http-proxy, making it suitable for basic proxying needs.
Express proxy middleware to forward request to another host and pass response back
$ npm install express-http-proxy --save
var proxy = require('express-http-proxy');
var app = require('express')();
app.use('/proxy', proxy('www.google.com', {
forwardPath: function(req, res) {
return require('url').parse(req.url).path;
}
}));
If you only want to proxy get request
app.use('/proxy', proxy('www.google.com', {
filter: function(req, res) {
return req.method == 'GET';
},
forwardPath: function(req, res) {
return require('url').parse(req.url).path;
}
}));
You can also intercept the response get by proxy before send it back to the client:
app.use('/proxy', proxy('www.google.com', {
forwardPath: function(req, res) {
return require('url').parse(req.url).path;
},
intercept: function(data, req, res, callback) {
data = JSON.parse(data.toString('utf8'));
callback(null, JSON.stringify(data.obj));
}
}));
MIT
FAQs
http proxy middleware for express
The npm package express-http-proxy receives a total of 498,502 weekly downloads. As such, express-http-proxy popularity was classified as popular.
We found that express-http-proxy demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.