mirror of
https://github.com/detleph/server.git
synced 2026-09-08 16:06:19 +02:00
Add controllers for 404 and the root endpoint
This commit is contained in:
@@ -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",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import roleSchemaRouter from "./Routes/role_schema.routes";
|
|||||||
import defaultErrorHandler from "./Middleware/error/handler";
|
import defaultErrorHandler from "./Middleware/error/handler";
|
||||||
import logger from "./Middleware/error/logger";
|
import logger from "./Middleware/error/logger";
|
||||||
import debugLogger from "./Middleware/debug/logger";
|
import debugLogger from "./Middleware/debug/logger";
|
||||||
|
import { notFoundHandler, rootHandler } from "./Middleware/error/defaultRoutes";
|
||||||
|
|
||||||
// Set up async error handling
|
// Set up async error handling
|
||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
@@ -79,9 +80,13 @@ async function main() {
|
|||||||
|
|
||||||
app.use("/api/role-schemas", roleSchemaRouter);
|
app.use("/api/role-schemas", roleSchemaRouter);
|
||||||
|
|
||||||
|
app.get("/", rootHandler);
|
||||||
|
|
||||||
// Error handling
|
// Error handling
|
||||||
app.use(defaultErrorHandler); // Not working
|
app.use(defaultErrorHandler); // Not working
|
||||||
|
|
||||||
|
app.use(notFoundHandler);
|
||||||
|
|
||||||
app.listen(process.env.PORT, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
logger.info(`Listening on port ${process.env.PORT}`);
|
logger.info(`Listening on port ${process.env.PORT}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user