Skip to content

Commit

Permalink
refactor index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jipsonminibhavan committed Feb 8, 2024
1 parent 543d7e9 commit 2e39050
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 49 deletions.
158 changes: 109 additions & 49 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<express.Router>) {
// 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<express.Router>): void {
// routes.forEach((route) => {
// this.app.use("./", route);
// });
// }

// private async databaseConnection(): Promise<void> {
// 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}`);
// });*/
74 changes: 74 additions & 0 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -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<express.Router>) {
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<express.Router>): void {
routes.forEach((route) => {
this.app.use("./", route);
});
}

private async databaseConnection(): Promise<void> {
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();

0 comments on commit 2e39050

Please sign in to comment.