add respond_with
This commit is contained in:
parent
9eb116daee
commit
0c290692f8
1 changed files with 35 additions and 0 deletions
35
respond_with.js
Normal file
35
respond_with.js
Normal file
|
@ -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 });
|
||||
},
|
||||
});
|
Loading…
Add table
Reference in a new issue