From 0c290692f84be626df442718f05a887529a17daa Mon Sep 17 00:00:00 2001 From: Caleb Webber Date: Tue, 9 Jul 2024 10:47:40 -0400 Subject: [PATCH] add respond_with --- respond_with.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 respond_with.js diff --git a/respond_with.js b/respond_with.js new file mode 100644 index 0000000..cfc7304 --- /dev/null +++ b/respond_with.js @@ -0,0 +1,35 @@ +#!bun +import { parseArgs } from "util"; + +/*** + * Usage: + * respond_with.js -p {{ PORT }} STATUS_CODE + * + * Defaults: + * PORT - 8080 + * STATUS_CODE - 200 +***/ +const args = parseArgs({ + args: Bun.argv, + options: { + p: { + type: 'string', + default: '8080' + } + }, + allowPositionals: true +}); + +console.debug(args); +const port = Number(args.values.p); +const lastPositional = Number(args.positionals[args.positionals.length - 1]); +const status = isNaN(lastPositional) ? 200 : lastPositional; +console.info(`serving on ${port}`); +console.info(`responding with ${status}`); + +Bun.serve({ + port, + fetch(req) { + return new Response("Bun!", { status }); + }, +});