-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (46 loc) · 1.48 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
const express = require("express")
const bodyParser = require("body-parser")
const cors = require("cors")
const dotenv = require("dotenv")
const app = express()
const detectMocha = require('detect-mocha');
// import routes
const hotels = require('./routes/hotels')
const hotelContacts = require('./routes/hotelContacts')
const invoices = require('./routes/invoices')
const orders = require('./routes/orders')
// connect to the database
const {connectDatabase} = require("./config/db")
const mongoose = require("mongoose");
dotenv.config()
const mongoUsername = process.env.MONGO_USERNAME;
const mongoPassword = process.env.MONGO_PASSWORD;
const databaseName = detectMocha() ? process.env.MONGO_TEST_DATABASE : process.env.MONGO_DATABASE;
connectDatabase(mongoUsername, mongoPassword, databaseName)
while (mongoose.connection.closed) {
console.log("xxx")
}
const port = process.env.PORT || 8080
const apiVersion = 'v1'
/**
* MIDDLE WARE
**/
// add cors middle ware
app.use(cors());
// parse requests of content-type - application/json
app.use(bodyParser.json());
app.use(express.json());
/**
* ROUTES
*/
app.use(`/api/${apiVersion}/hotels`, hotels)
app.use(`/api/${apiVersion}/hotel-contacts`, hotelContacts)
app.use(`/api/${apiVersion}/customers`, invoices)
app.use(`/api/${apiVersion}/orders`, orders)
app.listen(port, () => {
console.log(`api running on port ${port}`)
})
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
module.exports = app