-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (51 loc) · 1.56 KB
/
app.js
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
const express = require("express");
const path = require("path");
const cors = require("cors");
// Swagger configuration
const swaggerJsdoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const connection = require("./config/db");
const logger = require("./middleware/logger");
const usersRouter = require("./routes/users-route");
const blogRouter = require("./routes/blog-route");
const PORT = process.env.PORT || 3000;
const swaggerDefinition = {
openapi: "3.0.0",
info: {
title: "Express API for Blog Management Platform",
version: "1.0.0",
},
servers: [
{
url: "http://localhost:3000",
description: "Development server",
},
],
};
const options = {
swaggerDefinition,
apis: [path.join(__dirname, "./routes", "*.js")],
};
const swaggerSpec = swaggerJsdoc(options);
const app = express();
app.use(cors());
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
app.use(logger);
app.get("/", async (req, res) => {
res.send("Welcome to Blog Management Platform.");
});
app.use("/api/auth", usersRouter);
app.use("/api/posts", blogRouter);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
const server = app.listen(PORT, async () => {
try {
await connection;
console.log("Successfully connected to MONGODB.");
} catch (error) {
console.log("Failed to connect MONGODB due to error:", error);
}
console.log(`Server is up and running on ${PORT}`);
});
module.exports = { app, server };