-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
51 lines (44 loc) · 1.41 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
const express = require('express');
const mongoose = require('mongoose');
const winston = require('winston');
const expressWinston = require('express-winston');
const dotenv = require('dotenv');
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
app.use(express.json());
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,
}));
// 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);
});