Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deno - how to implement a 404 page

Tags:

deno

oak

I have been struggling to implement a 404 page in deno(oak server framework) - if i load any addtress that does not exist, i get just a blank page..

blank page sample

tried: (page404MiddleWare.ts):

import {Context, exists, send} from "./deps.ts";
export const page404MiddleWare = async (ctx: Context, next: Function) => {
   ctx.response.body = "404 page";
   await next();
}

But that seems like a bad practise.

like image 231
Daniel Avatar asked Sep 13 '20 13:09

Daniel


1 Answers

I would add a default route for all non existing urls and redirect user there:

router.get("/(.*)", async (context: Context) => {      
    context.response.status = 404;
    context.response.body = "404 | Page not Found";
});

and all rest routes:

...
...

router.get(
  "/api/users",
  UserController.fetch,
);

router.get(
  "/api/me",
  UserController.me,
);
...
...

Checkout my Deno REST boilerplate project for more details: https://github.com/vicky-gonsalves/deno_rest

like image 89
Vicky Gonsalves Avatar answered Nov 12 '22 11:11

Vicky Gonsalves