-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouting.js
More file actions
75 lines (55 loc) · 1.61 KB
/
routing.js
File metadata and controls
75 lines (55 loc) · 1.61 KB
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
const express = require('express')
const path = require('path')
const twilio = require('twilio')
const Datamodel = require("./model.js")
require ('dotenv').config()
const client = new twilio(process.env.SID, process.env.TOKEN);
routes = express.Router()
client.logLevel = 'debug';
//all the router come here
function sendsms(mess){
client.messages.create({
body:mess,
from:"+13256670177",
to:"+919148521359"
}).then(message=>{
console.log(message)
}).catch(err=>{console.log(err)})
}
routes.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'frontend/index1.html'));
});
//home page route
routes.get('/api',(req,res)=>{
async function datafromdb(){
let updatedval = await Datamodel.find({})
res.json(updatedval)
}
datafromdb()
})
//update page route
routes.post('/post',(req,res)=>{
var fired = Number(req.body.fire);
var gasd= Number(req.body.gas);
var waterd = Number(req.body.water);
var ledd = Number(req.body.led);
console.log([fired,gasd,waterd,ledd])
//validation and twilio came here
//twilio
if(fired === 1){
sendsms("fire is detected in home")
}
if(gasd === 1){
sendsms("gas is leaking in home")
}
if(waterd===1){
sendsms("water is full in home")
}
async function datatodb(){
let datan = await Datamodel.findOneAndUpdate({_id:"625a8cb40b0b6f9c25372ae2"},{firedata:fired,gasdata:gasd,waterlevel:waterd,led:ledd})
let updatedval = await Datamodel.find({})
res.send(updatedval)
}
datatodb()
})
module.exports = routes