Pengenalan
NexaUI Framework sekarang mendukung server Node.js bersamaan dengan PHP server. Anda dapat menjalankan kedua server secara bersamaan untuk aplikasi full-stack modern.
- ✅ 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 --versionJika 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 nodePerintah 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 kehttp://127.0.0.1(port 80), mengabaikan.nxdom-php-port.nxdom node dev/nxdom node dev [port_node]— proxy kehttp://127.0.0.1:8080.nxdom dev— PHP built-in (jendela baru) + Node; port PHP dari.nxdom-php-portatau 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 4000Server 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 40003. Jalankan Server (Production)
Untuk production/server online dengan PM2:
# Install PM2 (sekali saja)
npm install -g pm2
# Start production mode
nxdom node productionPerintah 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 both5. Uninstall Node.js Server
Untuk menghapus Node.js server (hanya di development mode):
nxdom uninstall nodeAPP_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 pointpackage.json- Dependencies configurationnode_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=developmentWorkflow 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 startupKeamanan
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 :::3000Solusi: Gunakan port lain atau kill process yang menggunakan port tersebut.
netstat -ano | findstr :3000
taskkill /PID <PID> /FNode.js tidak ditemukan
Solusi:
- Install Node.js dari https://nodejs.org/
- Restart terminal
- Verifikasi:
node --version
PM2 tidak ditemukan
Solusi:
- Install PM2:
npm install -g pm2 - Restart terminal (atau restart PowerShell)
- Verifikasi:
pm2 --version
Dokumentasi Lengkap
- Install & Setup - Panduan instalasi detail
- API Proxy - Cara menggunakan proxy ke PHP
- Static Files - Serving images, videos, dan assets
- Production Deployment - Deploy ke server online
Next Steps
- Pelajari cara menggunakan API Proxy
- Pelajari cara serving Static Files
- Setup Production Deployment dengan PM2
- Tambahkan custom API endpoints di
server.js - Integrasi dengan database (MySQL, MongoDB, dll)