img2db/index.ts

29 lines
698 B
TypeScript
Raw Normal View History

2024-04-04 01:18:32 +00:00
import { getImages, getImage, saveImage } from "./routes";
const server = Bun.serve({
port: 8080,
async fetch(req) {
const path = new URL(req.url).pathname;
// send back the image paths uploaded
if (path === "/images") {
return await getImages(req);
}
// send back the image that was uploaded
if (path.split("/")[1] === "image") {
return await getImage(req);
}
// receive image upload fromt the client
if (req.method === "POST" && path === "/upload") {
return await saveImage(req);
}
// default to sending back the web app
return new Response(Bun.file("./index.html"));
},
});
console.log(`Listening on ${server.url}`);