Install & Setup v2.0.0

Panduan Instalasi Node.js Server — Langkah demi langkah setup Node.js server di NexaUI Framework, dari install dependencies hingga running server pertama kali.

Langkah 1: Prasyarat

Install Node.js

Download dan install Node.js dari https://nodejs.org/

💡 Rekomendasi: Pilih versi LTS (Long Term Support) untuk stabilitas maksimal.

Verifikasi Instalasi

node --version # Output: v20.x.x atau lebih tinggi npm --version # Output: 10.x.x atau lebih tinggi

Jika command tidak ditemukan, restart terminal atau tambahkan Node.js ke PATH.

Langkah 2: Install Node.js Server

Jalankan perintah berikut di root project:

cd C:\Tnserver\www nxdom install node
cd /path/to/project ./nxdom install node

Apa yang Terjadi?

Perintah nxdom install node akan:

  1. ✅ Check apakah Node.js dan npm terinstall
  2. ✅ Check apakah server.js dan package.json sudah ada
  3. ✅ Prompt konfirmasi jika file sudah ada (overwrite atau skip)
  4. ✅ Membuat file server.js dari template
  5. ✅ Membuat file package.json dari template
  6. ✅ Menjalankan npm install untuk install dependencies

File yang Dibuat

1. server.js

File utama Node.js server dengan Express dan proxy ke PHP API.

const express = require('express'); const { createProxyMiddleware } = require('http-proxy-middleware'); const cors = require('cors'); // Di project nyata: PHP_SERVER dari resolvePhpServer() // (.nxdom-php-port → PHP_SERVER → http://127.0.0.1:8000) const app = express(); const PORT = process.env.PORT || 3000; app.use(cors()); app.use(express.json()); app.use('/nx', createProxyMiddleware({ target: process.env.PHP_SERVER || 'http://127.0.0.1:8000', changeOrigin: true, pathRewrite: { '^/nx': '/api' } })); app.get('/api/health', (req, res) => { res.json({ status: 'ok', timestamp: new Date() }); }); app.listen(PORT, () => { console.log(`Node.js server on port ${PORT}`); });

2. package.json

Konfigurasi dependencies dan scripts.

{ "name": "nexaui", "version": "1.0.0", "main": "server.js", "scripts": { "start": "node server.js", "dev": "nodemon server.js" }, "dependencies": { "express": "^4.18.2", "http-proxy-middleware": "^2.0.6", "cors": "^2.8.5", "helmet": "^7.1.0", "morgan": "^1.10.0" } }

3. node_modules/

Folder berisi semua dependencies yang terinstall. Folder ini di-ignore oleh Git.

Langkah 3: Konfigurasi .env

Buka file .env untuk dokumentasi konfigurasi Anda. server.js bawaan tidak memuat .env otomatis — nilai seperti PHP_SERVER diambil dari environment proses (termasuk yang diset nxdom.bat saat nxdom node).

Contoh isi referensi:

# Node.js Server Configuration PORT=3000 PHP_SERVER=http://127.0.0.1:8000 NODE_ENV=development # App Environment (untuk security) APP_ENV=development
Variable Default Deskripsi
PORT 3000 Port untuk Node.js server
PHP_SERVER http://127.0.0.1:8000 (jika tidak ada file port) Origin PHP untuk proxy; di server.js asli dipilih via resolvePhpServer()
NODE_ENV development Environment mode Node.js
APP_ENV development Environment app (untuk security check)

Langkah 4: Jalankan Server

Development Mode

nxdom node # atau port Node lain, proxy ke Apache :80: nxdom node 4000

Output:

============================================ Starting Node.js Development Server ============================================ [INFO] Port: 3000 [INFO] Starting server... Node.js server running on port 3000 [OK] Server started successfully! [INFO] Press Ctrl+C to stop

Production Mode

nxdom node production

Output:

============================================ Starting Node.js Production Server (PM2) ============================================ [INFO] Creating ecosystem.config.js... [OK] ecosystem.config.js created [INFO] Starting PM2 process... [PM2] Starting server.js in cluster mode (2 instances) [PM2] Done. [OK] Production server started! [INFO] Management commands: pm2 status pm2 logs nxdom-node pm2 restart nxdom-node pm2 stop nxdom-node

Langkah 5: Test Server

Buka browser atau gunakan curl untuk test endpoints:

# Node.js health check http://localhost:3000/api/health # PHP API via proxy http://localhost:3000/nx/test
curl http://localhost:3000/api/health curl http://localhost:3000/nx/test

Langkah 6: Jalankan PHP + Node.js Bersamaan

Disarankan (Windows): satu perintah — PHP built-in di jendela baru + Node di terminal saat ini:

nxdom dev

Atau Apache sudah menjalankan PHP (mis. http://localhost/api/...): jalankan saja nxdom node — proxy mengarah ke http://127.0.0.1. Jangan mengandalkan kombinasi nxdom start + nxdom node untuk menyamakan port built-in, karena nxdom node (bat) tidak membaca .nxdom-php-port untuk target proxy.

Setelah berjalan:

  • ✅ PHP (built-in atau Apache sesuai setup Anda)
  • ✅ Node.js API: http://localhost:3000/api/health
  • ✅ PHP via proxy Node: http://localhost:3000/nx/...

Uninstall (Optional)

Jika ingin menghapus Node.js server:

nxdom uninstall node
⚠️ Peringatan:
  • Perintah ini akan menghapus server.js, package.json, node_modules/, dan ecosystem.config.js
  • Hanya dapat dijalankan di mode APP_ENV=development
  • Akan menghentikan semua Node.js processes terlebih dahulu
  • Memerlukan konfirmasi sebelum menghapus

Troubleshooting Instalasi

Node.js tidak ditemukan

[ERROR] Node.js is not installed [INFO] Please install Node.js from https://nodejs.org/

Solusi:

  1. Download Node.js installer dari https://nodejs.org/
  2. Jalankan installer dan ikuti petunjuk
  3. Restart terminal/PowerShell
  4. Verifikasi: node --version

npm install gagal

npm ERR! network timeout

Solusi:

  1. Check koneksi internet
  2. Coba lagi: npm install
  3. Gunakan registry mirror: npm install --registry=https://registry.npmmirror.com

File sudah ada

[WARNING] server.js already exists Do you want to overwrite? (Y/N):

Pilihan:

  • Y - Overwrite file yang ada (backup manual dulu jika perlu)
  • N - Skip, gunakan file yang sudah ada

Permission denied (Linux/macOS)

bash: ./nexa: Permission denied

Solusi:

chmod +x nexa ./nxdom install node

Struktur File Setelah Install

C:\Tnserver\www\ ├── server.js # Node.js server (baru) ├── package.json # Dependencies config (baru) ├── node_modules/ # Dependencies (baru) ├── .env # Environment variables ├── .gitignore # Ignore node_modules ├── nxdom.bat # CLI Windows ├── nexa # CLI Linux/macOS ├── controllers/ # PHP Controllers │ └── Api/ # PHP API Controllers ├── system/ │ └── bin/ │ ├── templates/ │ │ ├── server.js.template │ │ └── package.json.template │ ├── nxdom-install-node.bat │ └── nxdom-install-node.sh └── templates/ # Views

Dependencies yang Terinstall

Package Versi Deskripsi
express ^4.18.2 Web framework untuk Node.js
http-proxy-middleware ^2.0.6 Proxy middleware untuk routing ke PHP
cors ^2.8.5 Enable Cross-Origin Resource Sharing
helmet ^7.1.0 Security headers middleware
morgan ^1.10.0 HTTP request logger
dotenv ^16.3.1 Load environment variables dari .env
mysql2 ^3.6.0 MySQL client (optional)
nodemon ^3.0.1 Auto-restart saat development (devDependency)

Verifikasi Instalasi

1. Check File

dir server.js dir package.json dir node_modules
ls -la server.js ls -la package.json ls -la node_modules

2. Test Server

# Start server nxdom node # Di terminal lain, test endpoint curl http://localhost:3000/api/health

Response yang diharapkan:

{ "status": "ok", "message": "NexaUI Node.js server is running", "timestamp": "2026-03-31T10:30:00.000Z", "phpServer": "http://127.0.0.1:8000" }

Reinstall Dependencies

Jika node_modules/ terhapus atau corrupt:

# Hapus node_modules dan package-lock.json rm -rf node_modules package-lock.json # Install ulang npm install

Update Dependencies

Untuk update dependencies ke versi terbaru:

# Check outdated packages npm outdated # Update semua npm update # Update specific package npm install express@latest

Next Steps