This starts an HTTP server listening on port 3000. It demonstrates basic routing with a number of common responses and also handles POST data from standard forms or as JSON.
const server = Bun.serve({
async fetch(req) {
const path = new URL(req.url).pathname;
// respond with text/html
if (path === "/") return new Response("Welcome to Bun!");
// redirect
if (path === "/abc") return Response.redirect("/source", 301);
// send back a file (in this case, *this* file)
if (path === "/source") return new Response(Bun.file(import.meta.path));
// respond with JSON
if (path === "/api") return Response.json({ some: "buns", for: "you" });
// receive JSON data to a POST request
if (req.method === "POST" && path === "/api/post") {
const data = await req.json();
console.log("Received JSON:", data);
return Response.json({ success: true, data });
}
// receive POST data from a form
if (req.method === "POST" && path === "/form") {
const data = await req.formData();
console.log(data.get("someField"));
return new Response("Success");
}
// 404s
return new Response("Page not found", { status: 404 });
},
});
console.log(`Listening on ${server.url}`);
1---2name: bun-common-http-server-usage3description: Common HTTP server usage4---56# Common HTTP server usage78This starts an HTTP server listening on port `3000`. It demonstrates basic routing with a number of common responses and also handles POST data from standard forms or as JSON.910See [`Bun.serve`](/runtime/http/server) for details.1112```ts server.ts icon="https://mintcdn.com/bun-1dd33a4e/nIz6GtMH5K-dfXeV/icons/typescript.svg?fit=max&auto=format&n=nIz6GtMH5K-dfXeV&q=85&s=5d73d76daf7eb7b158469d8c30d349b0" theme={"theme":{"light":"github-light","dark":"dracula"}}13const server = Bun.serve({14 async fetch(req) {15 const path = new URL(req.url).pathname;1617 // respond with text/html18 if (path === "/") return new Response("Welcome to Bun!");1920 // redirect21 if (path === "/abc") return Response.redirect("/source", 301);2223 // send back a file (in this case, *this* file)24 if (path === "/source") return new Response(Bun.file(import.meta.path));2526 // respond with JSON27 if (path === "/api") return Response.json({ some: "buns", for: "you" });2829 // receive JSON data to a POST request30 if (req.method === "POST" && path === "/api/post") {31 const data = await req.json();32 console.log("Received JSON:", data);33 return Response.json({ success: true, data });34 }3536 // receive POST data from a form37 if (req.method === "POST" && path === "/form") {38 const data = await req.formData();39 console.log(data.get("someField"));40 return new Response("Success");41 }4243 // 404s44 return new Response("Page not found", { status: 404 });45 },46});4748console.log(`Listening on ${server.url}`);49```
Run npx skillmds@latest add jarle/bun-common-http-server-usage in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Common HTTP server usage It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
jarle (@jarle) published this skill. Their other Agent Skills are listed on their SkillMD profile.