Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions dev-proxy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import http from 'node:http';

// The remote server where your API and Database actually live
const GATEWAY_HOST = process.env.GATEWAY_HOST || '192.168.1.50'; // Change to your remote IP
const GATEWAY_PORT = 8080;

const server = http.createServer((req, res) => {
// 1. Manually handle CORS to allow your local Vite dev server (port 5173)
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

// Handle preflight requests
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}

// 2. Configure the proxy request to the remote backend
const options = {
hostname: GATEWAY_HOST,
port: GATEWAY_PORT,
path: req.url,
method: req.method,
headers: req.headers,
// CRITICAL: Force IPv4 to avoid the Node.js 24 + Vite 7 DNS resolution bug
family: 4
};

const proxyReq = http.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});

proxyReq.on('error', (err) => {
console.error('Proxy Error:', err.message);
res.writeHead(502);
res.end('Bad Gateway');
});

req.pipe(proxyReq);
});

// The proxy sits on 5174, listening for Vite's requests
server.listen(5174, '127.0.0.1', () => {
console.log(`🚀 Proxy bridging Local (5174) -> Remote (${GATEWAY_HOST}:${GATEWAY_PORT})`);
});
40 changes: 40 additions & 0 deletions dev-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
set -e

# --- Configuration ---
# Change this to your remote server's IP address
export GATEWAY_HOST=${GATEWAY_HOST:-"192.168.1.50"}
PROXY_PORT=5174
VITE_PORT=5173

# Get the directory where this script is located
DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$DIR"

# Cleanup function to kill background processes on exit
cleanup() {
echo ""
echo "🛑 Shutting down dev services..."
kill $PROXY_PID $VITE_PID 2>/dev/null || true
exit 0
}

# Trap interruption signals (Ctrl+C, termination, etc.)
trap cleanup EXIT INT TERM

echo "Starting Remote Backend Bridge..."
echo "🔗 Gateway: $GATEWAY_HOST"

# 1. Start the Proxy (Background)
# Explicitly using node to run the proxy script
node dev-proxy.mjs &
PROXY_PID=$!

# 2. Start Vite (Background)
# We point Vite's API base to our local proxy port
echo "🚀 Starting Vite on port $VITE_PORT..."
VITE_API_BASE=http://localhost:$PROXY_PORT npx vite --host 0.0.0.0 --port $VITE_PORT &
VITE_PID=$!

# Wait for processes to finish (POSIX compliant for macOS/Linux)
wait $PROXY_PID $VITE_PID