# ๐Ÿ”ง Troubleshooting Railway Deployment ## โŒ Error 502: Application Failed to Respond ### Penyebab Umum: 1. **Missing Dependencies** 2. **Port Binding Error** 3. **Database Connection Failed** 4. **Syntax Error di Code** 5. **Environment Variables Salah** --- ## ๐Ÿ” Cara Debug ### 1. Cek Railway Logs **Di Railway Dashboard:** 1. Go to your project 2. Click service name 3. Click "Deployments" 4. Click latest deployment 5. View "Deploy Logs" dan "Application Logs" **Cari error:** ``` Error: Cannot find module 'websocket-stream' Error: listen EADDRINUSE: address already in use :::1883 Error: connect ECONNREFUSED SyntaxError: Unexpected token ``` --- ## ๐Ÿ› ๏ธ Fix yang Sudah Dilakukan ### Fix 1: Remove `digitalRead()` Error **Error:** ```javascript relay1: digitalRead(RELAY1) === 'LOW', // โŒ Arduino function in Node.js ``` **Fix:** ```javascript relay1: latestData.relay1 || false, // โœ… Keep current status ``` **Status:** โœ… FIXED (commit b08ad68) --- ### Fix 2: Disable MQTT Broker **Error:** ``` Error: listen EADDRINUSE: address already in use :::1883 ``` **Penyebab:** Railway tidak support custom ports (1883, 8883) **Fix:** ```javascript if (process.env.ENABLE_MQTT_BROKER === 'true') { mqttServer.listen(MQTT_PORT, () => { console.log(`๐Ÿš€ MQTT Broker running`); }).on('error', (err) => { console.warn(`โš ๏ธ MQTT Broker failed`); }); } else { console.log('โ„น๏ธ MQTT Broker disabled'); } ``` **Status:** โœ… FIXED (commit b08ad68) --- ### Fix 3: Add Missing Dependency **Error:** ``` Error: Cannot find module 'websocket-stream' ``` **Penyebab:** `websocket-stream` digunakan di code tapi tidak ada di `package.json` **Fix:** ```json { "dependencies": { "websocket-stream": "^5.5.2" // โœ… Added } } ``` **Status:** โœ… FIXED (commit 794201c) --- ## ๐Ÿงช Test Deployment ### Tunggu 2-3 Menit Railway perlu waktu untuk: 1. Pull code dari GitHub 2. Install dependencies (`npm install`) 3. Build (jika perlu) 4. Start server (`npm start`) ### Test Health Check ```powershell # PowerShell Invoke-WebRequest -Uri "https://web-production-47eb.up.railway.app/" -UseBasicParsing # Expected Response: # StatusCode: 200 # Content: {"status":"OK","message":"Pengering Ikan Backend Server",...} ``` ### Test Database ```powershell Invoke-WebRequest -Uri "https://web-production-47eb.up.railway.app/api/database/test" -UseBasicParsing # Expected Response: # {"success":true,"message":"Database connection successful"} ``` --- ## ๐Ÿ“Š Expected Railway Logs ### Successful Deployment: ``` Building... โœ“ Dependencies installed โœ“ Build completed Starting... ๐Ÿ“ฆ Using MYSQL_URL for connection ๐Ÿ”ง Database Config: { host: 'mysql.railway.internal', ... } โœ… Database connected successfully โœ… Database tables initialized โ„น๏ธ MQTT Broker disabled (use external MQTT broker like HiveMQ) โ„น๏ธ WebSocket MQTT disabled ๐Ÿš€ REST API running on port 3000 โœ… Server is ready! ``` ### Failed Deployment: ``` Building... โœ“ Dependencies installed Starting... Error: Cannot find module 'xxx' at Function.Module._resolveFilename ... Application exited with code 1 ``` --- ## ๐Ÿ”„ Jika Masih Error 502 ### 1. Cek Railway Logs Lihat error message di logs. ### 2. Cek Environment Variables Pastikan ada: ``` MYSQL_URL=mysql://root:...@mysql.railway.internal:3306/railway NODE_ENV=production PORT=3000 ``` ### 3. Cek Dependencies Pastikan semua dependencies ada di `package.json`: ```json { "dependencies": { "express": "^4.18.2", "cors": "^2.8.5", "aedes": "^0.51.3", "ws": "^8.16.0", "websocket-stream": "^5.5.2", // โ† Harus ada! "dotenv": "^16.4.5", "morgan": "^1.10.0", "mysql2": "^3.9.1" } } ``` ### 4. Test Locally ```bash cd backend npm install npm start # Jika error, fix dulu sebelum push ``` ### 5. Restart Railway Service Di Railway Dashboard: - Click service - Click "Settings" - Click "Restart" --- ## ๐Ÿ“ Checklist Deployment - [x] Fix `digitalRead()` error - [x] Disable MQTT broker - [x] Add `websocket-stream` dependency - [ ] Wait for Railway deploy (2-3 menit) - [ ] Test health check endpoint - [ ] Test database connection - [ ] Initialize database tables - [ ] Test with ESP32 --- ## ๐ŸŽฏ Next Steps ### 1. Tunggu Deploy Selesai Railway sedang deploy dengan fix terbaru. ### 2. Test Endpoints ```powershell cd C:\Users\abilh\aplikasi_novil\backend .\test-railway.ps1 ``` ### 3. Cek Logs Jika masih error, cek Railway logs untuk error message. ### 4. Initialize Database ```powershell Invoke-WebRequest -Uri "https://web-production-47eb.up.railway.app/api/database/init" -Method POST -UseBasicParsing ``` --- ## ๐Ÿ†˜ Jika Masih Gagal ### Option 1: Simplify Server Buat versi minimal tanpa MQTT: ```javascript const express = require('express'); const app = express(); app.get('/', (req, res) => { res.json({ status: 'OK' }); }); app.listen(3000, () => { console.log('Server running'); }); ``` Test apakah ini bisa deploy. Jika bisa, tambahkan fitur satu per satu. ### Option 2: Check Railway Status Cek https://status.railway.app/ untuk service outage. ### Option 3: Redeploy Di Railway Dashboard: - Click "Deployments" - Click "Redeploy" pada deployment terakhir --- ## ๐Ÿ“š Resources - **Railway Docs:** https://docs.railway.app/ - **Railway Logs:** Railway Dashboard โ†’ Deployments โ†’ View Logs - **GitHub Repo:** https://github.com/novil04/backend_aplikasi_novil --- ## โœ… Current Status **Latest Commit:** 794201c - Add missing websocket-stream dependency **Deploy Status:** โณ Deploying... **Expected Time:** 2-3 minutes Tunggu deploy selesai, lalu test! ๐Ÿš€