This repository has been archived by the owner on Jul 11, 2020. It is now read-only.
forked from EmergAlert/FrontEnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (56 loc) · 1.74 KB
/
server.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
/*
Modules
*/
const express = require('express');
const path = require('path');
require('dotenv').config();
var request = require('request');
var twilio = require('twilio');
var accountSid = process.env.ACCOUNTSID; // Your Account SID from www.twilio.com/console
var authToken = process.env.AUTHTOKEN; // Your Auth Token from www.twilio.com/console
var client = require('twilio')(accountSid, authToken);
const MessagingResponse = require('twilio').twiml.MessagingResponse;
/*
Express Setup
*/
const app = express();
app.use(express.static('web'));
app.listen(3000, function() {
console.log('Server is running. Access using localhost:3000.');
});
/*
Logger
*/
var logger = function(req, res, next) {
console.log('%s %s', req.method, req.url);
next();
}
app.use(logger);
/*
Routes
*/
function html(page) {
return path.join(__dirname, 'web', page + '.html')
}
app.get('/', function(req, res) {
res.sendFile(html('index'));
});
app.get('/test', function(req, res) {
res.sendFile(html('test'));
});
app.post('/alert', function(req, res) {
console.log("Alert detected");
res.end();
request('https://api.mlab.com/api/1/databases/athenahacks/collections/students?apiKey='+process.env.API_KEY, { json: true }, (err, res, main) => {
if (err) { return console.log(err); }
console.log(main.length + " Success!") ;
for(var i = 0; i < main.length; i++){
client.messages.create({
body: "This is an alert!! not really this is Diane testing. there is a shooting going on in the school. Stay safe.\n PS: Not sorry for disturbing.",
to: main[i]["phoneNumber"], // Text this number
from: process.env.SENDERNUM // From a valid Twilio number
})
.then((message) => console.log(message.sid));
}
})
})