-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (135 loc) · 4.09 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var http = require('http'),
debug = require('debug')('http'),
express = require('express'),
path = require('path'),
Mongoose = require('mongoose'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
bodyParser = require('body-parser'),
LaunchController = require('./controllers/launchController'),
RocketController = require('./controllers/rocketController'),
icalendar = require('icalendar');
var mongoHost = 'localHost';
var mongoPort = 27017;
var mongoClient = new MongoClient(new Server(mongoHost, mongoPort));
var rocketController;
var launchController;
mongoClient.open(function(err, mongoClient) {
if (!mongoClient) {
console.error("Error! Exiting... Must start MongoDB first");
process.exit(1);
}
var db = mongoClient.db("test");
rocketController = new RocketController(db);
launchController = new LaunchController(db);
});
var app = express();
var app1 = express();
app1.set('port', 8080);
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.json());
app.use(function(req, res, next) {
console.log('%s @ %s: %s %s', req.headers.host, req.headers['x-real-ip'],
req.method, req.url);
next();
});
app.get('/', function (req, res) {
res.send("ARIK RULES!!");
});
app.get('/rocket/manufacturer/:manufacturer', function(req, res) {
rocketController.getManufacturer(req.params.manufacturer, function (err, rockets) {
if (err) {
res.send(400, err);
} else {
console.log(rockets);
res.json(rockets);
}
})
});
app.get('/rocket/model/:model', function(req, res) {
rocketController.getModel(req.params.model, function (err, rockets) {
if (err) {
res.send(400, err);
} else {
console.log(rockets);
res.json(rockets);
}
})
});
app.post('/rocket/', function (req, res) {
rocketController.create(req.body, function(err, doc) {
if (err) {
res.err(404);
} else {
res.send(doc);
}
})
});
app.get('/launch/', function(req, res) {
launchController.getAll(function (err, launches) {
if (err) {
res.send(400, err);
} else {
res.json(launches);
}
})
})
app.post('/launch/', function (req, res) {
launchController.create(req.body, function(err, doc) {
if (err) {
res.err(404);
} else {
res.send(doc);
}
})
});
app.post('/testDate/', function(req, res) {
var db = mongoClient.db("test");
var d = new Date(req.body.date);
if (!isNaN(d.getTime())) {
db.collection('TEST', function (err, testCollection) {
if (!err) {
testCollection.insert({'date': d}, function (err) {
});
}
});
res.send("whatever");
}
else {
console.log("received wrong date format");
res.send("date format error");
}
});
app.use(function(req, res) {
res.send(req.url);
});
app1.use(function(req, res, next) {
console.log('%s @ %s: %s %s', req.headers.host, req.headers['x-real-ip'],
req.method, req.url);
next();
});
app1.get('/',function(req, res) {
var ical = new icalendar.iCalendar();
launchController.getAll(function(err, launches) {
if (err) {
res.send(500);
} else {
for (i = 0, leng = launches.length; i < leng; ++i) {
var event = new icalendar.VEvent();
event.setDate(launches[i].Date, 60*60);
event.setSummary(launches[i].Mission + '-' + launches[i].Rocket.Model + launches[i].Rocket.Version);
ical.addComponent(event);
}
res.send(ical.toString());
}
});
})
app1.use(function(req, res) {
res.send(404);
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
http.createServer(app1).listen(app1.get('port'), function(){
console.log('Express server listening on port ' + app1.get('port'));
});