Skip to content

Commit

Permalink
✨ (server): Handle not found
Browse files Browse the repository at this point in the history
  • Loading branch information
toridoriv committed Oct 11, 2023
1 parent 40144de commit d17a86c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/server/server.middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { difference, type express, expressHandlebars } from "@deps";
import { difference, type express, expressHandlebars, Status } from "@deps";
import { retrieveHelpers } from "./server.utils.ts";

export function inspectRequest(
Expand Down Expand Up @@ -27,6 +27,10 @@ export const handlebars = expressHandlebars.create({
helpers: await retrieveHelpers(),
});

export function handleNotFound(req: express.Request, res: express.Response) {
return res.status(Status.NotFound).render("not-found", { path: req.path });
}

declare global {
namespace Express {
interface Request {
Expand Down
1 change: 1 addition & 0 deletions app/server/server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ application.use(express.json());
application.use(AssetsRouter);
application.use(WebpageRouter);
application.use("/api", ApiRouter);
application.use(middleware.handleNotFound);

await database.connect();

Expand Down
2 changes: 2 additions & 0 deletions app/views/not-found.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h2>404</h2>
<h3>Oops! Page <code>{{path}}</code> not found</h3>

0 comments on commit d17a86c

Please sign in to comment.