From 2e3905042e68d81b6afa41e9474ffbbfd2fcd75d Mon Sep 17 00:00:00 2001 From: Jipson Minibhavan Date: Thu, 8 Feb 2024 13:30:45 +0100 Subject: [PATCH] refactor index.ts --- backend/src/index.ts | 158 +++++++++++++++++++++++++++++------------- backend/src/server.ts | 74 ++++++++++++++++++++ 2 files changed, 183 insertions(+), 49 deletions(-) create mode 100644 backend/src/server.ts diff --git a/backend/src/index.ts b/backend/src/index.ts index 2ff284b..c9f0a96 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,53 +1,113 @@ -import express, { Request, Response } from "express"; -import bodyParser from "body-parser"; -import cors from "cors"; -import connectToDB from "./db"; -import config from "./config"; -import passport from "passport"; -import { Strategy as LocalStrategy } from "passport-local"; -import { UserModel } from "./models/Users"; -import initializePassport from "./middleware/auth"; -import session from "express-session"; +import App from "./server"; import userRoutes from "./routes/userRoutes"; import statusRoutes from "./routes/statusRoutes"; import linkRoutes from "./routes/linkRoutes"; -const app = express(); -app.use(express.json()); - -//Connection to mongodb -connectToDB(); - -app.use(cors()); - -app.use(bodyParser.json()); -app.use(bodyParser.urlencoded({ extended: false })); - -app.get("/corstest", (req: Request, res: Response) => { - res.json({ - status: "ok", - }); -}); - -app.use( - session({ - secret: config.PASSPORT_SECRET, - resave: false, - saveUninitialized: false, - }) -); -const passportMiddleware = initializePassport(); -app.use(passportMiddleware.initialize()); - -passport.use(new LocalStrategy(UserModel.authenticate())); -passport.serializeUser(UserModel.serializeUser()); -passport.deserializeUser(UserModel.deserializeUser()); - -app.use("/", userRoutes); -app.use("/", statusRoutes); -app.use("/", linkRoutes); - -//Express-Server -app.listen(config.PORT, () => { - console.log(`Server is running on port ${config.PORT}`); -}); +const app = new App([userRoutes, statusRoutes, linkRoutes]); + +app.listen(); + +// import express, { Request, Response } from "express"; +// import bodyParser from "body-parser"; +// import session from "express-session"; +// import cors from "cors"; +// import passport from "passport"; +// import { Strategy as LocalStrategy } from "passport-local"; + +// import connectToDB from "./db"; +// import config from "./config"; + +// import { UserModel } from "./models/Users"; +// import initializePassport from "./middleware/auth"; + +// class App { +// public app: express.Application; + +// constructor(routes: Array) { +// this.app = express(); +// this.initializeMiddleware(); +// this.initializePassport(); +// this.initializeRoutes(routes); +// this.databaseConnection(); +// } +// private initializeMiddleware(): void { +// this.app.use(cors()); +// this.app.use(bodyParser.json()); +// this.app.use(bodyParser.urlencoded({ extended: false })); +// this.app.use( +// session({ +// secret: config.PASSPORT_SECRET, +// resave: false, +// saveUninitialized: false, +// }) +// ); +// } +// private initializePassport(): void { +// const passportMiddleware = initializePassport(); +// this.app.use(passportMiddleware.initialize()); + +// passport.use(new LocalStrategy(UserModel.authenticate())); +// passport.serializeUser(UserModel.serializeUser()); +// passport.deserializeUser(UserModel.deserializeUser()); +// } + +// private initializeRoutes(routes: Array): void { +// routes.forEach((route) => { +// this.app.use("./", route); +// }); +// } + +// private async databaseConnection(): Promise { +// try { +// await connectToDB(); +// } catch (error) { +// console.error("MongoDB connection error:", error); +// } +// } +// public listen(): void { +// this.app.listen(config.PORT, () => { +// console.log(`Server on Port ${config.PORT}`); +// }); +// } +// } + +// export default App; +// /*const app = express(); +// app.use(express.json()); + +// //Connection to mongodb +// connectToDB(); + +// app.use(cors()); + +// app.use(bodyParser.json()); +// app.use(bodyParser.urlencoded({ extended: false })); + +// app.get("/corstest", (req: Request, res: Response) => { +// res.json({ +// status: "ok", +// }); +// }); + +// app.use( +// session({ +// secret: config.PASSPORT_SECRET, +// resave: false, +// saveUninitialized: false, +// }) +// ); +// const passportMiddleware = initializePassport(); +// app.use(passportMiddleware.initialize()); + +// passport.use(new LocalStrategy(UserModel.authenticate())); +// passport.serializeUser(UserModel.serializeUser()); +// passport.deserializeUser(UserModel.deserializeUser()); + +// app.use("/", userRoutes); +// app.use("/", statusRoutes); +// app.use("/", linkRoutes); + +// //Express-Server +// app.listen(config.PORT, () => { +// console.log(`Server is running on port ${config.PORT}`); +// });*/ diff --git a/backend/src/server.ts b/backend/src/server.ts new file mode 100644 index 0000000..8c56ed7 --- /dev/null +++ b/backend/src/server.ts @@ -0,0 +1,74 @@ +import express, { Request, Response } from "express"; +import bodyParser from "body-parser"; +import session from "express-session"; +import cors from "cors"; +import passport from "passport"; +import { Strategy as LocalStrategy } from "passport-local"; + +import connectToDB from "./db"; +import config from "./config"; + +import { UserModel } from "./models/Users"; +import initializePassport from "./middleware/auth"; + +class App { + public app: express.Application; + + constructor(routes: Array) { + this.app = express(); + this.initializeMiddleware(); + this.initializePassport(); + this.initializeRoutes(routes); + this.databaseConnection(); + } + private initializeMiddleware(): void { + this.app.use(cors()); + this.app.use(bodyParser.json()); + this.app.use(bodyParser.urlencoded({ extended: false })); + this.app.use( + session({ + secret: config.PASSPORT_SECRET, + resave: false, + saveUninitialized: false, + }) + ); + } + private initializePassport(): void { + const passportMiddleware = initializePassport(); + this.app.use(passportMiddleware.initialize()); + + passport.use(new LocalStrategy(UserModel.authenticate())); + passport.serializeUser(UserModel.serializeUser()); + passport.deserializeUser(UserModel.deserializeUser()); + } + + private initializeRoutes(routes: Array): void { + routes.forEach((route) => { + this.app.use("./", route); + }); + } + + private async databaseConnection(): Promise { + try { + await connectToDB(); + } catch (error) { + console.error("MongoDB connection error:", error); + } + } + public listen(): void { + this.app.listen(config.PORT, () => { + console.log(`Server on Port ${config.PORT}`); + }); + } +} + +export default App; + +// import App from "./index"; +// import userRoutes from "./routes/userRoutes"; +// import statusRoutes from "./routes/statusRoutes"; +// import linkRoutes from "./routes/linkRoutes"; + +// const app = new App([userRoutes, statusRoutes, linkRoutes]); + +// app.listen();