-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (55 loc) · 1.81 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
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
71
72
73
74
75
76
77
78
79
80
81
// Importing packages in our application
const fs = require('fs')
const express = require('express')
const mongoose = require('mongoose')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const appConfig = require('./config/appConfig')
const routeLogger = require('./middlewares/routeLogger');
//Creating application
const app = express()
//middlewares
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(routeLogger.logIp);
//Bootstrap models
let modelsPath = './models'
fs.readdirSync(modelsPath).forEach(function (file){
if (~file.indexOf('.js')){
console.log(file)
require(modelsPath + '/' + file)
}
})
//End Bootstrap models
//Bootstrap Routes
let routesPath = './routes'
fs.readdirSync(routesPath).forEach(function (file){
if (~file.indexOf('.js')){
console.log('Including the following files')
console.log(routesPath + '/' + file)
let route = require(routesPath +'/' + file)
route.setRouter(app)
}
})
//End Bootstrap Routes
//Listening the server - creating a local server
app.listen(appConfig.port, () => {
console.log('Example app listening on port 3000!');
//Creating the mongo db connection here
let db = mongoose.connect(appConfig.db.uri, { useMongoClient: true });
})
//Handling Mongoose connection error
mongoose.connection.on('error', function (err) {
console.log('database connection error');
console.log(err)
}); // end mongoose connection error
//Handling Mongoose success event
mongoose.connection.on('open', function (err) {
if (err) {
console.log("database error");
console.log(err);
} else {
console.log("database connection open success");
}
}); // end mongoose connection open handler