-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (70 loc) · 2.1 KB
/
app.js
File metadata and controls
78 lines (70 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const path = require("path");
const port = process.env.PORT || 5000;
// Connect to MongoDB
const mongoURI = process.env.MONGODB_URI || "mongodb://localhost:27017/your_database";
mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("Connected to MongoDB");
})
.catch((error) => {
console.error("Error connecting to MongoDB:", error);
});
// Serve static files
app.use(express.static(path.join(__dirname, "public")));
// Extended: https://swagger.io/specification/#infoObject
const swaggerOptions = {
swaggerDefinition: {
info: {
version: "1.0.0",
title: "Customer API",
description: "Customer API Information",
contact: {
name: "Amazing Developer",
},
servers: [`http://localhost:${port}`],
},
tags: [
{
name: "Todos",
description: "Operations related to todos",
},
{
name: "Customers",
description: "Operations related to customers",
},
{
name: "Products",
description: "Operations related to Products",
},
{
name: "Orders",
description: "Operations related to Orders",
},
],
},
// ['.routes/*.js']
apis: ["./routes/*.js"],
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Home page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Routes
const todoRouter = require("./routes/todoRouter");
const customerRouter = require("./routes/customerRouter");
const orderRouter = require("./routes/orderRouter");
const productRouter = require("./routes/productRouter");
app.use("/todos", todoRouter);
app.use("/customers", customerRouter);
app.use("/products", productRouter);
app.use("/orders", orderRouter);
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});