Skip to content

Instantly share code, notes, and snippets.

@Hexagon
Created September 15, 2023 18:23
Show Gist options
  • Save Hexagon/9dd71c353163e87b2d2d0e6b509f3353 to your computer and use it in GitHub Desktop.
Save Hexagon/9dd71c353163e87b2d2d0e6b509f3353 to your computer and use it in GitHub Desktop.
// ---------------------------------------------------------
// Bun.serve
const server = Bun.serve({
port: 0,
fetch: async (req) => {
const url = new URL(req.url);
switch (url.pathname) {
case "/json":
const jsonResponse = {
message: "Hello, World!",
number: 5,
literal: `(${4}+${4})*${21.2}/${2}=${84.8}`
};
return new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
"content-type": "application/json; charset=utf-8",
}
});
break;
default:
return new Response("Not found", {
status: 404
});
break;
}
}
});
// ---------------------------------------------------------
// Deno.serve
// Start a HTTP server using Deno.serve
const server = Deno.serve(
{
port: 0
},
async (req) => {
const url = new URL(req.url);
switch (url.pathname) {
case "/json":
const jsonResponse = {
message: "Hello, World!",
number: 5,
literal: `(${4}+${4})*${21.2}/${2}=${84.8}`
};
return new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
"content-type": "application/json; charset=utf-8",
}
});
break;
default:
return new Response("Not found", {
status: 404
});
break;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment