-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
111 lines (88 loc) · 2.63 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Npm modules
var express = require('express');
// Mongoose added
var mongoose = require('mongoose');
require('mongoosefromclass')(mongoose);
//Fake JSON Data
var salesData = require('./src/data/sales-data.json');
var brokersData = require('./src/data/brokers-data.json');
var usData = require('./src/data/us-data.json');
// Mongoose added
global.mongoose = mongoose;
// Mongoose added
// Stop mongoose from using an old promise library
// (takes away the warning "mpromise is deprecated")
mongoose.Promise = Promise;
// Load classes, make them global and then convert selected ones to modules
global.Restrouter = require('./modules/restrouter.class');
global.Sale = require('./modules/sale.class');
global.Sale = mongoose.fromClass(global.Sale);
global.Broker = require('./modules/broker.class');
global.Broker = mongoose.fromClass(global.Broker);
global.Us = require('./modules/us.class');
global.Us = mongoose.fromClass(global.Us);
// Create a new express server, store in the variable app
var app = express();
// Point to folders where we have static files
// (our frontend code)
app.use(express.static('src'));
app.use(express.static('./'));
new Restrouter(app, Sale);
new Restrouter(app, Us);
new Restrouter(app, Broker);
// Never cache request starting with "/rest/"
app.use((req, res, next)=>{
if(req.url.indexOf('/rest/') >= 0) {
res.set("Cache-Control", "no-store, must-revalidate");
}
next();
});
// If no other route rule fulfilled then return index.html
app.get('*',(req, res)=>{
res.sendFile(__dirname + '/src/index.html');
});
// Mongoose added
// Connect to mongoDB
// and when that is done start the express server
mongoose.connect('mongodb://127.0.0.1/realestate');
mongoose.connection.once('open', onceConnected);
function onceConnected() {
// Start the server
app.listen(3003, ()=>{
console.log('Express app listening on port 3003!');
});
// For each collection type we have JSON of
// If the db counts 0 of either item
// It will insert the JSON into the db.
createFakeDataFromJSON();
}
function createFakeDataFromJSON() {
Sale.count(function(err, count) {
if (count === 0) {
createDefaultSales();
}
});
Broker.count(function(err, count) {
if (count === 0) {
createDefaultBrokers();
}
});
Us.count(function(err, count) {
if (count === 0) {
createDefaultUs();
}
});
function createDefaultSales() {
salesData.forEach(function(data) {
new Sale(data).save();
});
}
function createDefaultBrokers() {
brokersData.forEach(function(data) {
new Broker(data).save();
});
}
function createDefaultUs() {
new Us(usData).save();
}
}