forked from patrickpischulti/test_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (80 loc) · 2.52 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
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
var interval;
var interval_switch;
var urlencodeParser = bodyParser.urlencoded({extended: false});
//Import
Simulation = require('./models/simulate');
SuitSwitch = require('./models/suitswitch');
//Database connector
mongoose.connect('mongodb://localhost/spacesuit');
//EJS framework for website display
app.set('view engine', 'ejs');
app.use('/assets', express.static('assets'));
//ROUTES
app.get('/',function(req, res){
res.render('index');
});
//On start button, simulation starts
app.post('/', urlencodeParser, function(req, res){
console.log("--------------Simulation started--------------")
var time = Date.now();
interval = setInterval(Simulation.suitTelemetry.bind(null, time),1000);
interval_switch = setInterval(SuitSwitch.SuitSwitch,1000);
res.render('contact',{qs: ""});
});
//Returns all simulated data from the database
app.get('/api/suit', function(req, res){
Simulation.getSuitTelemetry(function (err, data) {
if (err) {
throw err;
console.log(err);
}
res.json(data);
});
});
app.get('/api/suit/recent', function(req, res){
Simulation.getSuitTelemetryByDate(function (err, data) {
if (err) {
throw err;
console.log(err);
}
res.json(data);
});
});
app.get('/api/suitswitch', function(req, res){
SuitSwitch.getSuitSwitch(function (err, data) {
if (err) {
throw err;
console.log(err);
}
res.json(data);
});
});
app.get('/api/suitswitch/recent', function(req, res){
SuitSwitch.getSuitSwitchByDate(function (err, data) {
if (err) {
throw err;
console.log(err);
}
res.json(data);
});
});
app.get('/contact',function(req, res){
res.render('contact',{qs: req.query});
});
app.post('/contact', urlencodeParser, function(req, res){
//console.log(req.body);
console.log('--------------Simulation stopped--------------');
clearInterval(interval);
clearInterval(interval_switch);
res.render('contact-success',{data: req.body});
});
/* app.get('/profile/:name', function(req, res){
var data = {age:29, job: 'Astronaut', hobbies: ['eating', 'fighting', 'fishing']};
res.render('profile', {person: req.params.name, data:data});
}) */
app.listen(3000);
console.log('Server is running on port 3000...');