Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980const http = require("node:http");
// A small multi-file app whose markup uses absolute paths, as real apps do.const files = { "/style.css": ["text/css", `@import "/more.css";\n`], "/more.css": ["text/css", `#hi { color: rgb(1, 2, 3) }\n`], "/lib.js": ["text/javascript", `export const text = "from lib.js";\n`], "/app.js": ["text/javascript", `import { text } from "/lib.js";document.getElementById("js").textContent = text;fetch("/api/data").then((r) => r.json()).then((d) => { document.getElementById("api").textContent = String(d.n); });new Worker("/w.js").onmessage = (e) => { document.getElementById("worker").textContent = "worker:" + e.data; };`], "/w.js": ["text/javascript", `fetch("/api/data").then((r) => r.json()).then((d) => postMessage(d.n));\n`], "/api/data": ["application/json", JSON.stringify({ n: 42 })], "/about": ["text/html", `<h1 id="about-h">About</h1>`],};
const server = http.createServer();// A plain listener ahead of the async one: its throw is a true synchronous uncaught exception.server.on("request", (req) => { if (req.url === "/boom") throw new Error("boom");});server.on("request", async (req, res) => { if (req.url === "/async-boom") { await null; throw new Error("async boom"); } if (req.url === "/reject-string") { await null; throw "nope"; } if (req.method === "POST" && req.url === "/submit") { let body = ""; const dec = new TextDecoder(); req.on("data", (chunk) => { body += dec.decode(chunk, { stream: true }); }); req.on("end", () => { res.writeHead(200, { "content-type": "text/html; charset=utf-8" }); res.end(`<p id="submitted">${body}</p>`); }); return; } const file = req.method === "GET" && files[req.url]; if (file) { res.writeHead(200, { "content-type": `${file[0]}; charset=utf-8` }); res.end(file[1]); return; } if (req.method === "POST" && req.url.startsWith("/echo")) { let body = ""; const dec = new TextDecoder(); req.on("data", (chunk) => { body += dec.decode(chunk, { stream: true }); }); req.on("end", () => { res.setHeader("x-nib", "1"); res.writeHead(200, { "content-type": "text/plain; charset=utf-8" }); res.end(body); }); return; } if (req.url === "/bye") { res.writeHead(200, { "content-type": "text/plain; charset=utf-8" }); res.end("bye\n"); server.close(() => console.log("closed")); return; } res.writeHead(200, { "content-type": "text/html; charset=utf-8" }); const hi = `<h1 id="hi">Hello from ${req.method} ${req.url}</h1>`; if (req.url !== "/") { res.end(hi); return; } res.end(`<!doctype html><link rel="stylesheet" href="/style.css"><script type="module" src="/app.js"></script>${hi}<p id="js"></p><p id="api"></p><p id="worker"></p><a id="about" href="/about">About</a><form id="f" method="post" action="/submit"><input name="q" value="hello"><button>Send</button></form>`);});server.listen(3000, () => console.log("listening on 3000"));