add funcs for better content handling

This commit is contained in:
2023-05-12 12:43:54 +02:00
parent 03a1f35eaa
commit e3e5f8c1d2
2 changed files with 49 additions and 1 deletions

13
mod.ts
View File

@@ -176,7 +176,7 @@ export class HTTPServer {
let handler = await route.handler(
routeRequest,
routeReply,
);
) ?? routeReply.body;
if (typeof (handler) == "object") {
handler = JSON.stringify(handler, null, 2);
@@ -374,6 +374,17 @@ export class RouteRequest {
export class RouteReply {
headers: Headers = new Headers();
statusCode: Status = Status.OK;
body: unknown;
json(json: JSON | { [key: string]: unknown } | []) {
this.type("application/json");
this.body = JSON.stringify(json, null, 2);
}
html(html: string) {
this.type("text/html");
this.body = html;
}
header(name: string, value: string): RouteReply {
this.headers.set(name, value);