Node.js Server v2.0.0

NexaUI Node.js Server — Server Node.js dengan Express untuk API endpoints, proxy ke PHP controllers, dan microservices. Mendukung development mode dan production mode dengan PM2.

Pengenalan

NexaUI Framework sekarang mendukung server Node.js bersamaan dengan PHP server. Anda dapat menjalankan kedua server secara bersamaan untuk aplikasi full-stack modern.

Fitur Utama:
  • ✅ Express.js web framework
  • ✅ Proxy otomatis ke PHP API controllers
  • ✅ Non-blocking & asynchronous
  • ✅ Development mode (simple start)
  • ✅ Production mode (PM2 cluster)
  • ✅ CORS enabled
  • ✅ Load balancing

Prasyarat

Sebelum menggunakan Node.js server, pastikan sudah terinstall:

# Check Node.js node --version # Check npm npm --version

Jika belum terinstall, download dari: https://nodejs.org/ (pilih versi LTS)

Perintah CLI

1. Install Node.js Server

Setup Node.js server untuk pertama kali:

nxdom install node
./nxdom install node

Perintah ini akan:

  • Membuat file server.js (Node.js server dengan proxy ke PHP)
  • Membuat file package.json (dependencies configuration)
  • Menginstall semua dependencies ke node_modules/

2. Jalankan Server (Development)

Di Windows (nxdom.bat):

  • nxdom node / nxdom node [port_node] — proxy PHP ke http://127.0.0.1 (port 80), mengabaikan .nxdom-php-port.
  • nxdom node dev / nxdom node dev [port_node] — proxy ke http://127.0.0.1:8080.
  • nxdom dev — PHP built-in (jendela baru) + Node; port PHP dari .nxdom-php-port atau 8000.
# Port Node default 3000 nxdom node # Port Node 4000, proxy ke Apache :80 nxdom node 4000 # Proxy ke PHP :8080 nxdom node dev nxdom node dev 4000

Server Node: http://localhost:3000 (atau port yang Anda set).

2b. PHP + Node — nxdom dev

Untuk menghindari error proxy ECONNREFUSED (Node aktif tanpa PHP), gunakan perintah ini setelah nxdom install node: PHP built-in dijalankan di jendela CMD baru, Node di terminal Anda. Port PHP mengikuti file .nxdom-php-port bila ada, jika tidak 8000.

nxdom dev # Port Node opsional (default 3000): nxdom dev 4000

3. Jalankan Server (Production)

Untuk production/server online dengan PM2:

# Install PM2 (sekali saja) npm install -g pm2 # Start production mode nxdom node production

Perintah ini akan:

  • Membuat file ecosystem.config.js (jika belum ada)
  • Menjalankan server dengan PM2 (cluster mode, 2 instances)
  • Auto-restart jika crash
  • Load balancing otomatis

4. Restart Server

# Restart Node.js saja nxdom restart node # Restart PHP saja nxdom restart php # Restart keduanya nxdom restart both

5. Uninstall Node.js Server

Untuk menghapus Node.js server (hanya di development mode):

nxdom uninstall node
⚠️ Keamanan: Perintah uninstall hanya dapat dijalankan di mode APP_ENV=development. Ini untuk mencegah penghapusan tidak sengaja di production server.

Endpoint API

Node.js Native API

Endpoint yang diproses langsung oleh Node.js:

Method Endpoint Deskripsi
GET /api/health Health check server
GET /api/test Test endpoint
GET /api/fast Fast response (instant)
GET /api/slow Slow response (5 seconds) - untuk test non-blocking

PHP API via Proxy

Endpoint yang di-proxy ke PHP server:

Node.js Request PHP Server Request Controller
GET /nx/test GET /api/test TestController.php
POST /nx/auth/login POST /api/auth/login AuthController.php
GET /nx/docs GET /api/docs DocsController.php
GET /nx/* GET /api/* Semua API Controllers

Contoh Penggunaan

Fetch dari JavaScript

// Node.js health check fetch('http://localhost:3000/api/health') .then(res => res.json()) .then(data => console.log(data)); // PHP API via proxy fetch('http://localhost:3000/nx/test') .then(res => res.json()) .then(data => console.log(data)); // POST request fetch('http://localhost:3000/nx/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: 'user@example.com', password: 'password' }) }) .then(res => res.json()) .then(data => console.log(data));

Menjalankan PHP + Node.js Bersamaan

Disarankan: nxdom dev (PHP built-in di jendela baru + Node). Jika PHP dilayani Apache di http://127.0.0.1, cukup nxdom node (Windows). Lihat indeks Platform untuk perbedaan nxdom node / nxdom node dev.

Development vs Production

Aspek Development Production
Command nxdom node nxdom node production
Process Manager ❌ Tidak ✅ PM2
Instances 1 2 (cluster)
Auto-restart
Background ❌ Foreground ✅ Background
Load Balancing
Monitoring ✅ PM2 monit

Non-Blocking & Asynchronous

Node.js server berjalan secara non-blocking dan asynchronous, artinya:

  • ✅ Semua request diproses bersamaan (concurrent)
  • ✅ UI client tidak freeze saat fetch API
  • ✅ Request cepat tidak perlu tunggu request lambat
  • ✅ Bisa handle ribuan request bersamaan
// Request 1: Slow (5 detik) fetch('http://localhost:3000/api/slow'); // Request 2: Fast (instant) - tidak perlu tunggu! fetch('http://localhost:3000/api/fast'); // ✅ Fast selesai duluan, tidak tunggu slow!

File yang Dibuat

Saat nxdom install node

  • server.js - Node.js server entry point
  • package.json - Dependencies configuration
  • node_modules/ - Installed dependencies

Saat nxdom node production

  • ecosystem.config.js - PM2 configuration (dibuat otomatis)

Environment Variables

PORT, PHP_SERVER, dan NXDOM_IGNORE_PHP_PORT_FILE dibaca dari environment proses. nxdom.bat menyetel beberapa di antaranya saat nxdom node / nxdom node dev. File .env tidak dimuat otomatis oleh server.js bawaan — gunakan untuk dokumentasi atau jika Anda menambahkan dotenv.

PORT=3000 PHP_SERVER=http://127.0.0.1:8000 APP_ENV=development NODE_ENV=development

Workflow Lengkap

# 1. Install nxdom install node # 2. Jalankan (Windows: lihat nxdom node / nxdom node dev) nxdom node # 3. Test # http://localhost:3000/api/health # http://localhost:3000/nx/test # 4. Stop (Ctrl+C) # 5. Uninstall (optional) nxdom uninstall node
# 1. Install nxdom install node # 2. Install PM2 npm install -g pm2 # 3. Start production nxdom node production # 4. Management pm2 status pm2 logs nxdom-node pm2 restart nxdom-node # 5. Save & auto-start pm2 save pm2 startup

Keamanan

🔒 Uninstall Protection:

Perintah nxdom uninstall node hanya dapat dijalankan di mode APP_ENV=development. Jika APP_ENV=production, perintah akan ditolak untuk mencegah penghapusan tidak sengaja di production server.

Troubleshooting

Port sudah digunakan

Error: listen EADDRINUSE: address already in use :::3000

Solusi: Gunakan port lain atau kill process yang menggunakan port tersebut.

netstat -ano | findstr :3000 taskkill /PID <PID> /F

Node.js tidak ditemukan

Solusi:

  1. Install Node.js dari https://nodejs.org/
  2. Restart terminal
  3. Verifikasi: node --version

PM2 tidak ditemukan

Solusi:

  1. Install PM2: npm install -g pm2
  2. Restart terminal (atau restart PowerShell)
  3. Verifikasi: pm2 --version

Dokumentasi Lengkap

Next Steps