Control how request paths are resolved and modified before proxying. The path resolution system allows dynamic path transformation with full async support.
Modify the request path before issuing the proxy request. The path includes everything after the host, including query parameters.
/**
* Function to resolve request path before proxying
* @param {object} req - Express request object
* @returns {string|Promise<string>} The resolved path including query parameters
*/
proxyReqPathResolver: (req) => string | Promise<string>;Usage Examples:
const proxy = require('express-http-proxy');
// Simple path modification
app.use('/api', proxy('localhost:8080', {
proxyReqPathResolver: function (req) {
// Replace '/api' with '/v1'
return req.url.replace('/api', '/v1');
}
}));
// Query parameter handling
app.use('/search', proxy('search.example.com', {
proxyReqPathResolver: function (req) {
const parts = req.url.split('?');
const queryString = parts[1];
const updatedPath = parts[0].replace(/test/, 'prod');
return updatedPath + (queryString ? '?' + queryString : '');
}
}));Use Promises for asynchronous path resolution operations.
/**
* Promise-based path resolver for async operations
* @param {object} req - Express request object
* @returns {Promise<string>} Promise resolving to the final path
*/
proxyReqPathResolver: (req) => Promise<string>;Usage Examples:
// Async path resolution
app.use('/dynamic', proxy('api.example.com', {
proxyReqPathResolver: function(req) {
return new Promise((resolve, reject) => {
// Simulate async lookup (database, cache, etc.)
setTimeout(() => {
const userId = req.headers['x-user-id'];
const resolvedPath = `/users/${userId}${req.url}`;
resolve(resolvedPath);
}, 100);
});
}
}));
// Database-driven path resolution
app.use('/tenant', proxy('multi-tenant.example.com', {
proxyReqPathResolver: async function(req) {
const tenantId = req.headers['x-tenant-id'];
const tenant = await db.getTenant(tenantId);
return `/${tenant.subdomain}${req.url}`;
}
}));Build complete URLs with proper encoding and parameter handling.
/**
* Complex path resolver with URL construction
* @param {object} req - Express request object with full URL context
* @returns {string} Constructed path with proper encoding
*/
proxyReqPathResolver: (req) => string;Usage Examples:
// Advanced URL construction
app.use('/forward', proxy('backend.example.com', {
proxyReqPathResolver: function(req) {
const url = new URL(req.url, 'http://dummy');
// Add API version to path
const pathSegments = url.pathname.split('/').filter(Boolean);
pathSegments.unshift('v2', 'api');
// Modify query parameters
url.searchParams.set('source', 'proxy');
url.searchParams.set('timestamp', Date.now().toString());
return '/' + pathSegments.join('/') + url.search;
}
}));Legacy path forwarding function. Use proxyReqPathResolver instead.
/**
* @deprecated Use proxyReqPathResolver instead
*/
forwardPath?: (req) => string;Legacy async path forwarding function. Use proxyReqPathResolver instead.
/**
* @deprecated Use proxyReqPathResolver instead
*/
forwardPathAsync?: (req, callback) => void;