diff --git a/README.md b/README.md index 28a6056..3a38e93 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Edit di Cursor di Mac, commit, lalu `git push`. Deploy ke `ubuntu-vm` terpisah ( | Folder | Pelajaran | Port di VM | |--------|-----------|------------| | `pelajaran-1/` | Nginx + HTML statis | `8088` | -| `pelajaran-2/` | Node.js di Docker | `8091` | +| `pelajaran-2/` | Node.js di Docker — lihat `pelajaran-2/PELAJARAN-2.md` | `8091` | ## Git remote diff --git a/pelajaran-2/server.js b/pelajaran-2/server.js index 9499c5c..0d05a22 100644 --- a/pelajaran-2/server.js +++ b/pelajaran-2/server.js @@ -1,13 +1,112 @@ const http = require('http'); + +const notes = []; + // fungsi ini dipanggil ketika ada request function handleRequest(request, response) { console.log('Ada yang minta:', request.url); const waktu = new Date().toISOString("id-ID"); + // --- CABANG API --- + if (request.url === '/api/hello') { + response.writeHead(200, { + 'Content-Type': 'application/json; charset=utf-8' + }) + response.end( + JSON.stringify({ + message: 'Halo dari API Node', + waktu: waktu, + }) + ) + return; // penting: berhenti disini, jangan kirim HTML juga. + } + + if (request.url === '/api/greet' && request.method === 'POST') { + let body = ''; + + request.on('data', function(chunk) { + body += chunk; + }) + + request.on('end', function() { + let name = 'teman'; + try { + const parsed = JSON.parse(body); + if (parsed.name) { + name = String(parsed.name) + } + } catch (err) { + console.error('Gagal membaca body:', err); + } + + response.writeHead(200, { + 'Content-Type': 'application/json; charset=utf-8' + }) + response.end( + JSON.stringify({ + sapaan: `Halo, ${name}!`, + waktu: new Date().toISOString(), + }) + ) + }) + return; + } + + if (request.url === '/api/notes' && request.method === 'GET') { + response.writeHead(200, { + 'Content-Type': 'application/json; charset=utf-8', + }) + response.end(JSON.stringify(notes)); + return; + } + + if (request.url === '/api/notes' && request.method === 'POST') { + let body = ''; + + request.on('data', function(chunk) { + body += chunk; + }) + request.on('end', function() { + let teks = ''; + try { + const parsed = JSON.parse(body); + if (parsed.teks) { + teks = String(parsed.teks).trim(); + } + } catch (err) { + console.error('Gagal membaca body:', err); + } + + if (!teks) { + response.writeHead(400, { + 'Content-Type': 'application/json; charset=utf-8', + }) + response.end(JSON.stringify({ + error: 'Teks tidak boleh kosong', + })) + return; + } + + const note = { + id: Date.now(), + teks: teks, + waktu: new Date().toISOString(), + } + notes.push(note); + + response.writeHead(201, { + 'Content-Type': 'application/json; charset=utf-8', + }) + response.end(JSON.stringify(note)); + return; + }) + return; + } + // --- CABANG HTML --- response.writeHead(200, { - "Content-Type": "text/html; charset=utf-8", + 'Content-Type': 'text/html; charset=utf-8' }); response.end(` @@ -18,13 +117,95 @@ function handleRequest(request, response) {
Ini dibuat ulang setiap request, bukan file HTML diam.
-Waktu server: ${waktu}
-Path yang diminta: ${request.url}
Halaman ini dari server. Tombol di bawah memanggil API dari browser.
+Waktu saat halaman dibuat: ${waktu}
+ +Belum dipanggil.
+Belum ada sapaan.
+