Add controllers for 404 and the root endpoint

This commit is contained in:
Stephan
2022-05-17 16:16:26 +02:00
parent 645baf1f8a
commit 3ec5f92429
2 changed files with 32 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
import { Request, Response } from "express";
// Only called when no other route matches
export function notFoundHandler(req: Request, res: Response) {
return res.status(404).json({
type: "error",
payload: {
message: `The ${req.method} HTTP method is implemented for '${req.path}'`,
_links: [
{
rel: "root",
href: "/api",
},
],
},
});
}
export function rootHandler(req: Request, res: Response) {
return res.status(200).json({
type: "success",
payload: {
message: "Detleph event API",
detail: "This is the API for the Detleph event system",
},
});
}
+5
View File
@@ -12,6 +12,7 @@ import roleSchemaRouter from "./Routes/role_schema.routes";
import defaultErrorHandler from "./Middleware/error/handler";
import logger from "./Middleware/error/logger";
import debugLogger from "./Middleware/debug/logger";
import { notFoundHandler, rootHandler } from "./Middleware/error/defaultRoutes";
// Set up async error handling
require("express-async-errors");
@@ -79,9 +80,13 @@ async function main() {
app.use("/api/role-schemas", roleSchemaRouter);
app.get("/", rootHandler);
// Error handling
app.use(defaultErrorHandler); // Not working
app.use(notFoundHandler);
app.listen(process.env.PORT, () => {
logger.info(`Listening on port ${process.env.PORT}`);
});