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
+1
View File
@@ -0,0 +1 @@
console.log('Halo dari node, bukan dari browser');
+14 -12
View File
@@ -1,10 +1,10 @@
const http = require("http"); const http = require('http');
const server = http.createServer((request, response) => { // fungsi ini dipanggil ketika ada request
// request = apa yang browser minta function handleRequest(request, response) {
// response = apa yang kita kirim balik console.log('Ada yang minta:', request.url);
const waktu = new Date().toLocaleString("id-ID"); const waktu = new Date().toISOString("id-ID");
response.writeHead(200, { response.writeHead(200, {
"Content-Type": "text/html; charset=utf-8", "Content-Type": "text/html; charset=utf-8",
@@ -17,14 +17,16 @@ const server = http.createServer((request, response) => {
<title>Hello Node</title> <title>Hello Node</title>
</head> </head>
<body> <body>
<h1>Hello dari Node di homelab</h1> <h1>Halo dari Node</h1>
<p>Ini bukan file HTML diam. Teks di bawah dibuat oleh JavaScript saat request datang.</p> <p>Ini dibuat ulang setiap request, bukan file HTML diam.</p>
<p>Waktu server sekarang: <strong>${waktu}</strong></p> <p>Waktu server: <strong>${waktu}</strong></p>
<p>Path yang diminta: <code>${request.url}</code></p> <p>Path yang diminta: <code>${request.url}</code></p>
</body> </body>
</html>`); </html>`);
}); }
server.listen(3000, () => { const server = http.createServer(handleRequest);
console.log("Server siap di port 3000");
}); server.listen(3000, function() {
console.log('Loket siap di httpp://127.0.0.1:3000')
})