-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (39 loc) · 1.46 KB
/
server.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
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`
});
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const morgan = require('morgan');
const swaggerUi = require('swagger-ui-express');
const specs = require('./swaggerConfig');
const app = express();
// Middleware
app.use(cors());
app.use(morgan('dev'));
app.use(express.json()); // For parsing application/json
//Swagger
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
// // Connect to DB
// mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true })
// .then(() => console.log('Connected to MongoDB!'))
// .catch(err => console.error('Could not connect to MongoDB...', err));
// Connect to DB
async function connectDB() {
try {
await mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true });
console.log('Connected to MongoDB!');
} catch (err) {
console.error('Could not connect to MongoDB...', err);
}
}
connectDB();
// Import Routes
const authRoute = require('./routes/auth'); // assuming you have auth.js file in routes directory
const todoRoute = require('./routes/todos'); // assuming you have todos.js file in routes directory
// Route Middlewares
app.use('/api/user', authRoute);
app.use('/api/todos', todoRoute);
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));