bouncy HTTP Proxy and Router

3.2.2 · abandoned · verified Wed Apr 22

bouncy is a low-level Node.js library that provides HTTP proxy and routing capabilities by wrapping `net.Server`. It allows programmatic redirection of raw HTTP traffic based on request headers (like `Host`), paths, or methods. Developers define routing logic within a callback function, gaining fine-grained control over request and response streams for tasks like load balancing or host-based multiplexing. Version `3.2.2` was published in October 2014, and the project shows no significant activity since then, indicating it is abandoned. It differs from higher-level HTTP frameworks by operating directly with Node.js's `net` and `tls` modules, offering a more primitive, stream-oriented approach to proxying.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `bouncy` to create a host-based HTTP router. It sets up two backend servers on ports 8001 and 8002, and a `bouncy` proxy on port 8000. Requests are routed to different backends based on the `Host` header, or handled directly by the proxy, showing basic routing and direct response capabilities.

const bouncy = require('bouncy');
const http = require('http');

// Create a simple backend server 1
const backend1 = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end(`Hello from backend 8001 (Host: ${req.headers.host}, Path: ${req.url})`);
});
backend1.listen(8001, () => console.log('Backend 8001 listening.'));

// Create a simple backend server 2
const backend2 = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end(`Hello from backend 8002 (Host: ${req.headers.host}, Path: ${req.url})`);
});
backend2.listen(8002, () => console.log('Backend 8002 listening.'));

// Create the bouncy proxy server
const proxyServer = bouncy(function (req, res, bounce) {
    console.log(`Incoming request for Host: ${req.headers.host}, URL: ${req.url}`);

    if (req.headers.host === 'beep.example.com') {
        console.log('Routing to 8001');
        bounce(8001);
    } else if (req.headers.host === 'boop.example.com') {
        console.log('Routing to 8002');
        bounce(8002, { path: '/custom-path' }); // Example of path modification
    } else if (req.url === '/admin') {
        // Example of direct response from proxy without bouncing
        res.statusCode = 200;
        res.end('Admin dashboard via proxy.');
    } else {
        console.log('No route found, sending 404.');
        res.statusCode = 404;
        res.end('No such host or route found.');
    }
});

proxyServer.listen(8000, () => console.log('Proxy server listening on port 8000.'));

console.log('\nTo test, use curl or modify your /etc/hosts file:\n');
console.log('  curl -H "Host: beep.example.com" http://localhost:8000');
console.log('  curl -H "Host: boop.example.com" http://localhost:8000');
console.log('  curl http://localhost:8000/admin');
console.log('  curl http://localhost:8000/unknown-host');

view raw JSON →