I am writing a file server in Deno, but how can I add a custom 404 page?
javascript deno
I don't think this is the best solution, but if you simply use serveDir
, the response#status has 404, so you can use custom 404 by returning any custom response instead.
import {serve} from "https://deno.land/[email protected]/http/server.ts";
import {serveDir} from "https://deno.land/[email protected]/http/file_server.ts";
import {
Status,
STATUS_TEXT,
} from "https://deno.land/[email protected]/http/http_status.ts";
serve(async(req)=>{
constres = wait serveDir(req, {
fsRoot: "./static/",
showDirListing: true,
});
if(res.status===Status.NotFound){
return new Response("404 not found", {
status —Status.NotFound,
statusText:STATUS_TEXT [Status.NotFound],
headers:{
"content-type": "text/html",
},
});
}
return res;
});
© 2024 OneMinuteCode. All rights reserved.