Pelajaran 2E: halaman node dinamis (waktu + path)

This commit is contained in:
sayudhalw
2026-07-16 21:45:42 +07:00
parent 5e1c4f797d
commit 4361e73791
3 changed files with 21 additions and 18 deletions
+19 -17
View File
@@ -1,30 +1,32 @@
const http = require("http");
const http = require('http');
const server = http.createServer((request, response) => {
// request = apa yang browser minta
// response = apa yang kita kirim balik
// fungsi ini dipanggil ketika ada request
function handleRequest(request, response) {
console.log('Ada yang minta:', request.url);
const waktu = new Date().toISOString("id-ID");
const waktu = new Date().toLocaleString("id-ID");
response.writeHead(200, {
"Content-Type": "text/html; charset=utf-8",
});
response.writeHead(200, {
"Content-Type": "text/html; charset=utf-8",
});
response.end(`<!DOCTYPE html>
response.end(`<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8" />
<title>Hello Node</title>
</head>
<body>
<h1>Hello dari Node di homelab</h1>
<p>Ini bukan file HTML diam. Teks di bawah dibuat oleh JavaScript saat request datang.</p>
<p>Waktu server sekarang: <strong>${waktu}</strong></p>
<h1>Halo dari Node</h1>
<p>Ini dibuat ulang setiap request, bukan file HTML diam.</p>
<p>Waktu server: <strong>${waktu}</strong></p>
<p>Path yang diminta: <code>${request.url}</code></p>
</body>
</html>`);
});
}
server.listen(3000, () => {
console.log("Server siap di port 3000");
});
const server = http.createServer(handleRequest);
server.listen(3000, function() {
console.log('Loket siap di httpp://127.0.0.1:3000')
})