koa-http-proxy fully supports streaming! Just configure it properly.
app.use('/upload', proxy('fileserver.com', {
parseReqBody: false, // Enable streaming mode
retry: false, // Optional: explicitly disable retry
limit: '500mb' // Support large files
}));app.use('/api', proxy('api.server.com', {
parseReqBody: true, // Default: buffer mode
retry: true // Support retry
}));app.use(proxy('backend.com', {
parseReqBody: function(ctx) {
const size = parseInt(ctx.headers['content-length'] || '0');
const isUpload = ctx.path.includes('/upload');
// Use streaming for large files or uploads
if (size > 10 * 1024 * 1024 || isUpload) {
return false; // streaming mode
}
return true; // buffer mode (supports retry)
},
retry: async (handle, ctx) => {
// Only executes in buffer mode
return await smartRetryLogic(handle, ctx);
}
}));| Feature | Streaming Mode | Buffer Mode |
|---|---|---|
| Configuration | parseReqBody: false |
parseReqBody: true |
| Large File Support | ✅ Unlimited size | ❌ Memory limited |
| Memory Usage | ✅ Constant (streaming) | |
| Retry Support | ❌ Auto-disabled | ✅ Full support |
| Real-time Streaming | ✅ True streaming | ❌ Buffered transmission |
| Use Cases | File uploads, video streams, big data | API calls, JSON, small payloads |
- File uploads/downloads
- Video/audio streaming
- Large dataset transfers
- Real-time data streams
- IoT data transmission
- Log streaming
- REST API calls
- JSON data exchange
- Small file transfers (<10MB)
- Scenarios requiring retry mechanisms
- Scenarios requiring request body modification
- Large File Detection: Auto-disable retry for files >20MB
- Streaming Mode Detection: Auto-disable retry when
parseReqBody: false - Memory Monitoring: Prevent OOM errors
- Smart Warnings: Clear configuration issue notifications
[koa-http-proxy] Streaming mode detected (parseReqBody: false). Retry disabled for stream safety.
[koa-http-proxy] Body size exceeds cache limit (25MB > 20MB). Retries disabled to prevent OOM.
// File upload routes - use streaming
app.use('/files', proxy('fileserver.com', {
parseReqBody: false,
limit: '1gb'
}));
// API routes - use buffer + retry
app.use('/api', proxy('apiserver.com', {
parseReqBody: true,
retry: true
}));app.use(proxy('backend.com', {
parseReqBody: (ctx) => {
// Check content type
if (ctx.headers['content-type']?.includes('multipart/form-data')) {
return false; // streaming for file uploads
}
// Check path
if (ctx.path.startsWith('/upload') || ctx.path.startsWith('/stream')) {
return false; // streaming for specific paths
}
// Check size
const size = parseInt(ctx.headers['content-length'] || '0');
if (size > 5 * 1024 * 1024) { // 5MB
return false; // streaming for large content
}
return true; // buffer mode for everything else
}
}));app.use(proxy('backend.com', {
parseReqBody: (ctx) => {
const size = parseInt(ctx.headers['content-length'] || '0');
const mode = size > 10 * 1024 * 1024 ? 'streaming' : 'buffer';
// Log mode selection for monitoring
console.log(`${ctx.method} ${ctx.path}: ${mode} mode (${Math.round(size/1024)}KB)`);
return mode === 'buffer';
},
retry: async (handle, ctx) => {
const startTime = Date.now();
let result;
try {
result = await handle();
console.log(`API call success: ${ctx.path} (${Date.now() - startTime}ms)`);
} catch (error) {
console.log(`API call failed: ${ctx.path} (${error.message})`);
throw error;
}
return result;
}
}));Solution:
// Ensure streaming mode is used
app.use('/upload', proxy('fileserver.com', {
parseReqBody: false, // Key: enable streaming
limit: '500mb', // Set appropriate size limit
timeout: 120000 // Increase timeout
}));Check:
- Is
parseReqBody: falseset? (This disables retry) - Is file size >20MB? (Auto-disables retry)
- Is the result properly returned?
Solution:
// Enable streaming for large files
app.use(proxy('backend.com', {
parseReqBody: (ctx) => {
const size = parseInt(ctx.headers['content-length'] || '0');
return size < 1024 * 1024; // Use buffer for <1MB, streaming for >1MB
}
}));- ✅ Full Streaming Support: Set
parseReqBody: false - ✅ Two Modes Coexist: Can use both modes in the same application
- ✅ Automatic Safety Mechanisms: Prevent memory overflow and configuration errors
- ✅ Flexible Configuration: Support conditional logic for automatic mode selection
⚠️ Streaming and Retry are Mutually Exclusive: This is a safety design consideration