-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.03 KB
/
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
38
39
40
41
42
43
44
// Base
const express = require('express');
const api = express();
// Mongoose
const mongoose = require('mongoose');
// CORS
const cors = require('cors');
api.use(cors());
api.options('*', cors());
// Environment Variables
require('dotenv').config();
// MongoDB Connection
const connectionString = process.env.DB_CONNECTION_STRING;
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
}).catch(err => console.error(err)); // This handles initial connection errors
mongoose.connection.once('open', () => {
console.log('Database connection successful!');
});
// This handles errors after initial connection was established
mongoose.connection.on('error', err => {
console.error(err);
});
// Middleware
api.use(express.json());
api.use(express.urlencoded({ extended: true }));
// Route
const userRoutes = require('./routes/user');
api.use('/api/users', userRoutes);
// Listening Port
api.listen(process.env.PORT, () => {
console.log(`Listening on port ${ process.env.PORT }.`);
});