-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
190 lines (158 loc) · 3.89 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Created by tal on 10/2/16.
*/
var express = require('express');
var path = require('path');
var fs = require('fs');
var sendSMS = require('./twilio-run');
var app = express();
var handleLyft = require('./handleLyft.js')();
const PORT = process.env.PORT || 8080;
app.get('/', function (req, res) {
const FILE = './index.html';
res.statusCode = 200;
// res.sendFile();
// res.write("Working");
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
"Content-Type": "text/html",
'x-timestamp': Date.now(),
'x-sent': true
}
};
res.sendFile(FILE, options, function (err) {
if (err) {
// console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:', FILE);
res.end();
}
});
// sendSMS("4435591587", "Hi")
console.log("main");
});
app.get('/logo.png', function(req, res) {
const FILE = path.join(__dirname + "/logo.png");
res.statusCode = 200;
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
"Content-Type": "image/png",
'x-timestamp': Date.now(),
'x-sent': true
}
};
res.sendFile(FILE, options, function (err) {
if (err) {
console.log("An error occurred while attempting to serve " + FILE);
// console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:' + FILE);
res.end();
}
});
});
app.get('/images/*', function (req, res) {
allowServeFromDir(req, res, 'jpg');
});
app.get('/js/*', function (req, res) {
allowServeFromDir(req, res, 'js');
});
app.get('/css/*', function (req, res) {
allowServeFromDir(req, res, 'css');
});
app.get('/fonts/*', function (req, res) {
allowServeFromDir(req, res, 'font');
});
app.get('/trafficRisk_files/*', function (req, res) {
allowServeFromDir(req, res, 'js');
});
app.get('/dataPredict/trafficRisk_files/*', function (req, res) {
allowServeFromDir(req, res, 'js');
});
app.get('/dataPredict/trafficRisk.html', function(req, res) {
allowServeFromDir(req, res, 'html'); // HTML is not an expected value here, but the variable is html by default
});
app.get('/route.html', function(req, res) {
allowServeFromDir(req, res, 'html');
});
app.get('/welcome/*', function(req, res) {
var phone = req.url.split('/')[2];
console.log("New Alert User: " + phone);
sendSMS(phone, "Welcome to the BreezyRide alert system. We'll now let you know when you could be traveling more safely, quickly or efficiently.");
});
app.get('/icon/*', function(req, res) {
allowServeFromDir(req, res, '');
});
app.get('/favicon.ico', function(req, res) {
allowServeFromDir(req, res, '')
});
app.listen(PORT, function () {
console.log('Example app listening on port ' + PORT + '!');
});
function allowServeFromDir(req, res, type) {
var headerMIME = "text/html"; // This is a dangerous case, as it leaves html default
if (type == 'jpg') {
headerMIME = "image/jpeg";
} else if (type == 'js') {
headerMIME = "application/javascript";
} else if (type == 'css') {
headerMIME = "text/css";
} else if (type == 'font') {
headerMIME = "application/font-woff";
} else if (type == '') {
headerMIME = "";
}
const FILE = req.url;
res.statusCode = 200;
if (headerMIME) {
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
"Content-Type": headerMIME,
'x-timestamp': Date.now(),
'x-sent': true
}
};
} else {
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
}
res.sendFile(FILE, options, function (err) {
if (err) {
console.log("An error occurred while attempting to serve " + FILE);
// console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:' + FILE);
res.end();
}
});
}
function doIfFile(file, cb) {
fs.stat(file, function fsStat(err, stats) {
if (err) {
if (err.code === 'ENOENT') {
return cb(null, false);
} else {
return cb(err);
}
}
return cb(null, stats.isFile());
});
}