Skip to content

Instantly share code, notes, and snippets.

@dentropy
Created April 18, 2024 03:04
Show Gist options
  • Save dentropy/473c36a1a36e9dcecf204278e47f5905 to your computer and use it in GitHub Desktop.
Save dentropy/473c36a1a36e9dcecf204278e47f5905 to your computer and use it in GitHub Desktop.
import http from 'http';
import httpProxy from 'http-proxy';
// Create a proxy server with custom application logic
const proxy = httpProxy.createProxyServer({});
// Create your server that makes an operation that waits a while
// and then proxies the request
http.createServer((req, res) => {
// Modify headers
req.headers['Authorization'] = 'Bearer sk-56442f1e0c534aaaa22b9d34c8b55501';
// You can modify the target based on the incoming request,
// or by any other environmental factors, etc.
let target = {
host: 'ollama.local', // Target host
protocol: 'https:', // Target protocol (http: or https:)
port: 443 // Target port (80 for http, 443 for https)
};
// Change origin to true to update host header to target host
const options = {
target: target,
changeOrigin: true,
secure: true, // Set to false if you want to ignore SSL validation errors (not recommended)
};
// Proxy the request with the modified headers and target
proxy.web(req, res, options, (error) => {
if (error) {
console.error('Proxy error:', error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Error when connecting to proxy');
}
});
}).listen(8000);
console.log('Proxy server running on http://localhost:8000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment