-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (63 loc) · 1.85 KB
/
app.js
File metadata and controls
70 lines (63 loc) · 1.85 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
const express = require("express");
const mongoose = require("mongoose");
const winston = require("winston");
const expressWinston = require("express-winston");
const dotenv = require("dotenv");
const cors = require("cors");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger.json");
const authRoutes = require("./routes/authRoutes");
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware for logging HTTP requests and responses
app.use(express.json());
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(
expressWinston.logger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "combined.log" }),
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
),
meta: true,
msg: "HTTP {{req.method}} {{req.url}}",
expressFormat: true,
colorize: false,
// Customize logging to include user-specific logs
dynamicMeta: (req, res) => {
return {
userId: req.user ? req.user.id : "anonymous",
requestBody: req.body,
queryParameters: req.query,
responseStatus: res.statusCode,
responseBody: res.body,
};
},
})
);
// Routes
app.use("/auth", authRoutes);
// Swagger UI
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// Connect to MongoDB
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
winston.info(`Server is running on port ${PORT}`);
});
})
.catch((error) => {
console.error("Error connecting to MongoDB:", error);
winston.error("Error connecting to MongoDB:", error);
});
module.exports = app;