File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -729,6 +729,45 @@ app.use(async (context, next) => {
729729await app .listen ({ port: 8000 });
730730```
731731
732+ When ` send() ` can't find a matching filesystem entry, it will throw an
733+ ` HttpError ` with its status set to ` 404 ` (` Status.NotFound ` ). This example
734+ illustrates handling that error in order to use another middleware (a fallback
735+ virtual filesystem) when ` send() ` can't find a match:
736+
737+ ``` ts
738+ import {
739+ Application ,
740+ HttpError ,
741+ Router ,
742+ Status ,
743+ } from " https://deno.land/x/oak/mod.ts" ;
744+
745+ const fallbackFilesystem = new Router ()
746+ .get (" /virtual_file.txt" , (context ) => {
747+ context .response .body = " Hello world" ;
748+ });
749+
750+ const app = new Application ()
751+ .use (async (context , next ) => {
752+ try {
753+ await context .send ({
754+ root: ` ${Deno .cwd ()}/examples/static ` ,
755+ index: " index.html" ,
756+ });
757+ } catch (ex ) {
758+ if (ex instanceof HttpError && ex .status === Status .NotFound ) {
759+ // send didn't find a matching filesystem entry
760+ return next ();
761+ }
762+ throw ex ;
763+ }
764+ })
765+ .use (fallbackFilesystem .routes ())
766+ .use (fallbackFilesystem .allowedMethods ());
767+
768+ await app .listen ({ port: 8000 });
769+ ```
770+
732771` send() ` automatically supports features like providing ` ETag ` and
733772` Last-Modified ` headers in the response as well as processing ` If-None-Match `
734773and ` If-Modified-Since ` headers in the request. This means when serving up
You can’t perform that action at this time.
0 commit comments