Skip to content

Commit 3f1072c

Browse files
authored
Merge pull request #5 from jolynloh/BE/mongodbsetup
Implement API for create and update question
2 parents d4f7478 + 3706b11 commit 3f1072c

File tree

11 files changed

+2479
-221
lines changed

11 files changed

+2479
-221
lines changed
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
import express, { Express, Request, Response } from "express";
1+
import express, { Request, Response } from "express";
22
import dotenv from "dotenv";
3-
import path from "path";
43
import swaggerUi from "swagger-ui-express";
54
import yaml from "yaml";
65
import fs from "fs";
76

8-
const envFilePath = path.join(
9-
path.resolve(path.dirname(path.dirname(__dirname))),
10-
".env",
11-
);
7+
import connectDB from "./config/db.ts";
8+
import questionRoutes from "./src/routes/questionRoutes.ts";
9+
10+
dotenv.config();
1211

1312
const file = fs.readFileSync("./swagger.yml", "utf-8");
1413
const swaggerDocument = yaml.parse(file);
1514

16-
dotenv.config({ path: envFilePath });
17-
18-
const app: Express = express();
15+
const app = express();
1916

20-
const PORT = process.env.QUESTION_SERVICE_PORT || 3000;
17+
connectDB();
2118

19+
app.use(express.json());
20+
app.use("/api", questionRoutes);
21+
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
2222
app.get("/", (req: Request, res: Response) => {
2323
res.status(200).json({ message: "Hello world from question service" });
2424
});
2525

26-
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
27-
28-
app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
26+
export default app;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import mongoose from "mongoose";
2+
import dotenv from "dotenv";
3+
4+
dotenv.config();
5+
6+
const connectDB = async () => {
7+
try {
8+
if (process.env.MONGO_URI == undefined) {
9+
throw new Error("MONGO_URI is undefined");
10+
}
11+
12+
await mongoose.connect(process.env.MONGO_URI);
13+
console.log("MongoDB connected");
14+
} catch (error) {
15+
console.error(error);
16+
process.exit(1);
17+
}
18+
};
19+
20+
export default connectDB;

0 commit comments

Comments
 (0)