-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (30 loc) · 886 Bytes
/
index.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
const dotenv = require('dotenv');
const express = require('express');
const connectDB = require('./config/dbConnect');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const morgan = require('morgan');
const cors = require('cors');
const mongoose = require('mongoose');
const routes = require('./routes/taskRouter');
dotenv.config({ path: '.env' });
const app = express();
const PORT = process.env.PORT;
// Database connection
connectDB();
// Middleware setup
app.use(express());
app.use(bodyParser.json());
app.use(cors());
app.use(helmet());
app.use(morgan('tiny'));
// Routes
app.use('/api', routes);
// Start the server
mongoose.connection.once('open', () => {
console.log('Connected to MongoDB');
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
});
mongoose.connection.on('error', err => {
console.log(err);
});