From 4b1b65fb57b59c01a8c5480db44f177e7d6d9752 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 18 Mar 2023 19:05:33 +0900 Subject: feat(nuldoc): support custom 404 in serve command --- nuldoc-src/commands/serve.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'nuldoc-src') diff --git a/nuldoc-src/commands/serve.ts b/nuldoc-src/commands/serve.ts index b66ad2d..ffb2020 100644 --- a/nuldoc-src/commands/serve.ts +++ b/nuldoc-src/commands/serve.ts @@ -1,6 +1,7 @@ -import { join } from "std/path/mod.ts"; import { serveDir } from "std/http/file_server.ts"; +import { Status, STATUS_TEXT } from "std/http/http_status.ts"; import { serve } from "std/http/server.ts"; +import { join } from "std/path/mod.ts"; import { Config } from "../config.ts"; export function runServeCommand(config: Config) { @@ -14,9 +15,21 @@ export function runServeCommand(config: Config) { await p.status(); console.log("rebuild"); } - return serveDir(req, { + const res = await serveDir(req, { fsRoot: rootDir, showIndex: true, }); + if (res.status !== Status.NotFound) { + return res; + } + + const notFoundHtml = await Deno.readTextFile(join(rootDir, "404.html")); + return new Response(notFoundHtml, { + status: Status.NotFound, + statusText: STATUS_TEXT[Status.NotFound], + headers: { + "content-type": "text/html", + }, + }); }); } -- cgit v1.2.3-70-g09d2