Skip to content

Commit

Permalink
added example controller and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cdleveille committed Oct 5, 2024
1 parent 2e307bd commit 567150d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/server/controllers/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NextFunction, Request, Response, Router } from "express";

export const helloRouter = Router();

helloRouter.get("/hello", (_req: Request, res: Response, next: NextFunction) => {
try {
res.json({ message: "hello from bun!" });
} catch (error) {
next(error);
}
});
1 change: 1 addition & 0 deletions src/server/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./hello";
7 changes: 6 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { createServer } from "http";
import nocache from "nocache";
import path from "path";

import { helloRouter } from "@controllers";
import { Config } from "@helpers";
import { errorHandler, notFound } from "@middleware";
import { connectToDatabase, initSocket, log } from "@services";

const { IS_PROD, HOST, PORT, RELOAD_PORT, SKIP_DB } = Config;
Expand Down Expand Up @@ -47,12 +49,15 @@ app.use(compression());
app.use(
cors({
origin: "*",
methods: ["GET"]
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"]
})
);
app.use(express.static(PUBLIC_DIR));
app.set("json spaces", 2);
app.disable("x-powered-by");
app.use(helloRouter);
app.use(notFound);
app.use(errorHandler);
const httpServer = createServer(app);
initSocket(httpServer);
httpServer.listen(PORT, () => {
Expand Down
10 changes: 10 additions & 0 deletions src/server/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NextFunction, Request, Response } from "express";

import { CustomError } from "@types";

export const errorHandler = (error: Error | CustomError, req: Request, res: Response, next: NextFunction) => {
if (!error) return next();
const statusCode = error instanceof CustomError ? error.statusCode : res.statusCode !== 200 ? res.statusCode : 500;
const errorMessage = (typeof error === "string" ? error : error.message) || "Internal Server Error";
res.status(statusCode).json({ error: errorMessage });
};
2 changes: 2 additions & 0 deletions src/server/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./errorHandler";
export * from "./notFound";
5 changes: 5 additions & 0 deletions src/server/middleware/notFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Request, Response } from "express";

export const notFound = (_req: Request, res: Response) => {
res.status(404).json({ message: "Not Found" });
};
10 changes: 10 additions & 0 deletions src/server/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ export type TUser = {

type ReverseMap<T> = T[keyof T];
export type TSocketEvent = ReverseMap<typeof SocketEvent>;

export class CustomError extends Error {
statusCode: number;

constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;
Object.setPrototypeOf(this, CustomError.prototype);
}
}

0 comments on commit 567150d

Please sign in to comment.