diff options
| author | Ritesh Ghosh <[email protected]> | 2023-08-01 13:09:50 +0530 |
|---|---|---|
| committer | Ritesh Ghosh <[email protected]> | 2023-08-01 13:09:50 +0530 |
| commit | 35e61131b391fed28233372ecd1124c607c0a13c (patch) | |
| tree | cf0df78ab4240e753be7c8abd69ae5c6fd2e29ff /src/server.ts | |
| download | aniwatch-api-35e61131b391fed28233372ecd1124c607c0a13c.tar.xz aniwatch-api-35e61131b391fed28233372ecd1124c607c0a13c.zip | |
initial commit
Diffstat (limited to 'src/server.ts')
| -rw-r--r-- | src/server.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/server.ts b/src/server.ts new file mode 100644 index 0000000..fe723bc --- /dev/null +++ b/src/server.ts @@ -0,0 +1,37 @@ +import express, { + Request, + Response, + NextFunction, + ErrorRequestHandler, + Application, +} from "express"; +import { config } from "dotenv"; +import morgan from "morgan"; +import createHttpError from "http-errors"; + +config(); +const app: Application = express(); +const PORT: number = Number(process.env.PORT) || 4000; + +app.use(morgan("dev")); + +app.get("/", (req: Request, res: Response) => { + res.json("hi there"); +}); + +app.use((req: Request, res: Response, next: NextFunction) => + next(createHttpError.NotFound()) +); + +const errorHandler: ErrorRequestHandler = (error, req, res, next) => { + const status = error?.status || 500; + res.status(status).json({ + status, + message: error?.message || "Something Went Wrong", + }); +}; +app.use(errorHandler); + +app.listen(PORT, () => { + console.log(`⚔ api @ http://localhost:${PORT}`); +}); |
