-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
83 lines (75 loc) · 2.1 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
require('dotenv/config')
require('mongodb')
const express = require('express')
const Vonage = require('@vonage/server-sdk')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
let app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(express.static('public'))
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const contactsSchema = new mongoose.Schema({
name: String,
number: Number
})
const Contacts = mongoose.model('Contacts', contactsSchema)
const vonage = new Vonage({
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
applicationId: process.env.APPLICATION_ID,
privateKey: process.env.PRIVATE_KEY
})
app.get('/', function(req, res){
res.sendFile('index.html')
})
app.post('/contacts', function(req, res) {
const contact = new Contacts({
name: req.body.name
})
contact.save()
res.redirect('/')
})
app.get('/contacts', function(req, res) {
Contacts.find({}, function(err, contacts) {
if (err) {
console.log(err)
} else {
res.json(contacts)
}
})
})
app.post('/alert', function(req, res) {
let long = req.body['coordinates']['long']
let lat = req.body['coordinates']['lat']
let contacts = req.body['contacts']
for (let i = 0; i <= contacts.length; i++) {
vonage.channel.send({
'type': 'sms',
"number": contacts[i].number
}, {
'type': 'sms',
"number": process.env.FROM_NUMBER
}, {
'content': {
'type': 'text',
'text': `SOS! Your friend is in an emergency! Their latitude is ${lat} and` +
`their longitude is ${long}!`
}
},
(err, data) => {
if (err) {
console.error(err)
} else {
console.log(data.message_uuid)
}
}
)
}
})
app.listen(process.env.PORT)