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 @@ -787,6 +787,45 @@ app.use(async (context) => {
787
787
await app .listen ({ port: 8000 });
788
788
```
789
789
790
+ When ` send() ` can't find a matching filesystem entry, it will throw an
791
+ ` HttpError ` with its status set to ` 404 ` (` Status.NotFound ` ). This example
792
+ illustrates handling that error in order to use another middleware (a fallback
793
+ virtual filesystem) when ` send() ` can't find a match:
794
+
795
+ ``` ts
796
+ import {
797
+ Application ,
798
+ HttpError ,
799
+ Router ,
800
+ Status ,
801
+ } from " https://deno.land/x/oak/mod.ts" ;
802
+
803
+ const fallbackFilesystem = new Router ()
804
+ .get (" /virtual_file.txt" , (context ) => {
805
+ context .response .body = " Hello world" ;
806
+ });
807
+
808
+ const app = new Application ()
809
+ .use (async (context , next ) => {
810
+ try {
811
+ await context .send ({
812
+ root: ` ${Deno .cwd ()}/examples/static ` ,
813
+ index: " index.html" ,
814
+ });
815
+ } catch (ex ) {
816
+ if (ex instanceof HttpError && ex .status === Status .NotFound ) {
817
+ // send didn't find a matching filesystem entry
818
+ return next ();
819
+ }
820
+ throw ex ;
821
+ }
822
+ })
823
+ .use (fallbackFilesystem .routes ())
824
+ .use (fallbackFilesystem .allowedMethods ());
825
+
826
+ await app .listen ({ port: 8000 });
827
+ ```
828
+
790
829
` send() ` automatically supports features like providing ` ETag ` and
791
830
` Last-Modified ` headers in the response as well as processing ` If-None-Match `
792
831
and ` If-Modified-Since ` headers in the request. This means when serving up
You can’t perform that action at this time.
0 commit comments