-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
61 lines (55 loc) · 1.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* This app uses bun (https://bun.sh)
*
* Bun is much, much faster than node.js and Deno!
*
* We'll build a simple xml builder with xmlbuilder2 and allow for users to interact
* with it using http requests.
*
* Bun has its own built in http server, so we'll use that.
*/
import { create } from "xmlbuilder2";
import { schemas } from "./lib/schemas";
/**
* This is the main entry point for the app
*
* We'll create a simple http server that returns an xml document
*/
const server = Bun.serve({
port: 3000,
async fetch() {
try {
const docObject = {
root: {
"@att": "val",
foo: {
bar: "foobar",
},
baz: {},
},
};
// parse the default xml document
const parsed = schemas.xml.doc.parse(docObject);
if (!parsed) {
throw new Error("Failed to parse the default xml document");
}
// create the xml document
const doc = create({ version: "1.0", encoding: "UTF-8" }).ele(parsed);
const xmlString = doc.end({ prettyPrint: true });
return new Response(xmlString, {
headers: {
"Content-Type": "application/xml",
},
});
} catch (e: any) {
const message = e.message || "An error occurred";
return new Response(message, {
status: 500,
headers: {
"Content-Type": "text/plain",
},
});
}
},
});
console.log(`Listening on http://localhost:${server.port}`);