/** * Custom Next.js server with WebSocket support and local network discovery * Run with: node --require ts-node/register server.ts * Or add to package.json: "dev": "ts-node server.ts" */ import { createServer } from 'http'; import { parse } from 'url'; import { networkInterfaces } from 'os'; import next from 'next'; import { initializeWebSocketServer } from './lib/websocket'; const dev = process.env.NODE_ENV !== 'production'; const hostname = process.env.HOSTNAME || 'localhost'; const port = parseInt(process.env.PORT || '3001', 10); /** * Get local IP address for the current machine * Prioritizes IPv4 addresses on the local network */ function getLocalIpAddress(): string | null { const interfaces = networkInterfaces(); for (const name of Object.keys(interfaces)) { for (const iface of interfaces[name] || []) { // Skip internal and non-IPv4 addresses if (iface.family === 'IPv4' && !iface.internal) { return iface.address; } } } return null; } const app = next({ dev, hostname, port }); const handle = app.getRequestHandler(); app.prepare().then(() => { const server = createServer(async (req, res) => { try { const parsedUrl = parse(req.url || '', true); await handle(req, res, parsedUrl); } catch (err) { console.error('Error handling request:', err); res.statusCode = 500; res.end('Internal server error'); } }); // Initialize WebSocket server initializeWebSocketServer(server); server.listen(port, (err?: Error) => { if (err) throw err; const localIp = getLocalIpAddress(); const localhostUrl = `http://localhost:${port}`; console.log('\n' + '='.repeat(60)); console.log('šŸ›ļø Bagsy API Server'); console.log('='.repeat(60)); console.log('\nšŸ“ Local URLs:'); console.log(` • Localhost: ${localhostUrl}`); if (localIp) { console.log(` • Local Network: http://${localIp}:${port}`); console.log(`\n ā„¹ļø Use the Local Network URL to connect from mobile app on same WiFi`); } console.log(`\n🌐 WebSocket: ws://localhost:${port}/api/ws`); if (localIp) { console.log(` ws://${localIp}:${port}/api/ws`); } console.log('\n' + '='.repeat(60)); console.log('šŸ“± Mobile App Configuration:'); console.log('='.repeat(60)); console.log('\nSet EXPO_PUBLIC_API_URL in apps/mobile/.env to:'); console.log(` EXPO_PUBLIC_API_URL=${localIp ? `http://${localIp}:${port}` : localhostUrl}`); console.log('\n' + '='.repeat(60)); console.log('šŸš€ Deployment Guide:'); console.log('='.repeat(60)); console.log(` Production Deployment: 1. Build the app: npm run build 2. Deploy to a cloud provider: - Vercel (recommended for Next.js): npx vercel deploy - Railway: railway link railway deploy - Render: render.com - create new Web Service - Heroku (legacy): git push heroku main 3. Set environment variables on your host: - BETTER_AUTH_SECRET (use: openssl rand -base64 32) - BETTER_AUTH_URL (e.g., https://api.yourdomain.com) - DATABASE_URL (PostgreSQL connection string) - OPENROUTER_API_KEY (for AI suggestions) 4. Update mobile app with production API URL: EXPO_PUBLIC_API_URL=https://api.yourdomain.com For more details, see: DEPLOYMENT_GUIDE.md `); console.log('='.repeat(60) + '\n'); }); });