| 1 |
const http = require('http'); |
| 2 |
const fs = require('fs').promises; |
| 3 |
|
| 4 |
const routes = { |
| 5 |
'/': (req, res) => { |
| 6 |
res.writeHead(200, {'Content-Type': 'text/html'}); |
| 7 |
res.end(`<!DOCTYPE html> |
| 8 |
<html> |
| 9 |
<head> |
| 10 |
<title>hx-stream demo</title> |
| 11 |
<script src="/htmx.js"></script> |
| 12 |
<script src="/ext/hx-stream.js"></script> |
| 13 |
<style> |
| 14 |
body { font-family: system-ui; max-width: 600px; margin: 40px auto; padding: 0 20px; } |
| 15 |
button { padding: 8px 16px; cursor: pointer; font-size: 14px; } |
| 16 |
#output { border: 1px solid #ddd; border-radius: 4px; padding: 16px; min-height: 100px; |
| 17 |
margin: 12px 0; white-space: pre-wrap; line-height: 1.6; } |
| 18 |
#status { color: #666; font-size: 14px; min-height: 20px; } |
| 19 |
</style> |
| 20 |
</head> |
| 21 |
<body> |
| 22 |
<h2>hx-swap="stream" demo</h2> |
| 23 |
|
| 24 |
<button hx-get="/ai-response" hx-swap="stream" hx-target="#output" hx-ext="stream"> |
| 25 |
Generate AI response |
| 26 |
</button> |
| 27 |
|
| 28 |
<button hx-get="/html-stream" hx-swap="stream" hx-target="#output" hx-ext="stream"> |
| 29 |
Stream HTML fragments |
| 30 |
</button> |
| 31 |
|
| 32 |
<div id="output"></div> |
| 33 |
<div id="status"></div> |
| 34 |
|
| 35 |
<script> |
| 36 |
let status = document.getElementById("status"); |
| 37 |
|
| 38 |
document.body.addEventListener("htmx:stream:start", () => { |
| 39 |
status.textContent = "Streaming..."; |
| 40 |
}); |
| 41 |
|
| 42 |
document.body.addEventListener("htmx:stream:end", e => { |
| 43 |
status.textContent = "Done (" + e.detail.loaded + " bytes)"; |
| 44 |
}); |
| 45 |
</script> |
| 46 |
</body> |
| 47 |
</html>`); |
| 48 |
}, |
| 49 |
|
| 50 |
'/htmx.js': async (req, res) => { |
| 51 |
res.writeHead(200, {'Content-Type': 'application/javascript'}); |
| 52 |
res.end(await fs.readFile('src/htmx.js')); |
| 53 |
}, |
| 54 |
|
| 55 |
'/ext/hx-stream.js': async (req, res) => { |
| 56 |
res.writeHead(200, {'Content-Type': 'application/javascript'}); |
| 57 |
res.end(await fs.readFile('src/ext/hx-stream.js')); |
| 58 |
}, |
| 59 |
|
| 60 |
|
| 61 |
'/ai-response': (req, res) => { |
| 62 |
res.writeHead(200, {'Content-Type': 'text/html'}); |
| 63 |
let words = `The key insight behind hypermedia-driven applications is that the server sends HTML, not JSON. This means the server controls the presentation layer, and the client simply renders what it receives. This approach dramatically simplifies the client-side code, eliminates an entire class of state synchronization bugs, and lets you build rich interactive applications with remarkably little JavaScript.`.split(' '); |
| 64 |
let i = 0; |
| 65 |
let interval = setInterval(() => { |
| 66 |
if (i < words.length) { |
| 67 |
res.write((i > 0 ? ' ' : '') + words[i]); |
| 68 |
i++; |
| 69 |
} else { |
| 70 |
clearInterval(interval); |
| 71 |
res.end(); |
| 72 |
} |
| 73 |
}, 50); |
| 74 |
req.on('close', () => clearInterval(interval)); |
| 75 |
}, |
| 76 |
|
| 77 |
|
| 78 |
'/html-stream': (req, res) => { |
| 79 |
res.writeHead(200, {'Content-Type': 'text/html'}); |
| 80 |
let items = [ |
| 81 |
'<p><strong>Step 1:</strong> Connecting to server...</p>', |
| 82 |
'<p><strong>Step 2:</strong> Authenticating...</p>', |
| 83 |
'<p><strong>Step 3:</strong> Fetching data...</p>', |
| 84 |
'<p><strong>Step 4:</strong> Processing results...</p>', |
| 85 |
'<p style="color: green"><strong>Done!</strong> All steps completed.</p>' |
| 86 |
]; |
| 87 |
let i = 0; |
| 88 |
let interval = setInterval(() => { |
| 89 |
if (i < items.length) { |
| 90 |
res.write(items[i]); |
| 91 |
i++; |
| 92 |
} else { |
| 93 |
clearInterval(interval); |
| 94 |
res.end(); |
| 95 |
} |
| 96 |
}, 500); |
| 97 |
req.on('close', () => clearInterval(interval)); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
http.createServer(async (req, res) => { |
| 102 |
let pathname = new URL(req.url, 'http://localhost').pathname; |
| 103 |
let handler = routes[pathname]; |
| 104 |
if (handler) { |
| 105 |
try { await handler(req, res); } |
| 106 |
catch (e) { if (!res.headersSent) { res.writeHead(500); res.end('Error'); } } |
| 107 |
} else { |
| 108 |
res.writeHead(404); res.end('Not Found'); |
| 109 |
} |
| 110 |
}).listen(3002, () => console.log('http://localhost:3002')); |