-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (122 loc) · 3.2 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
//jshint esversion:6
const mongoose = require("mongoose"); //backend for mongodb
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const _ = require("lodash");
var sid = "AC4818444fcf353850ab8b0f6edcfa90cb";
var auth_token = "dab241de09613f87491b8da2f1c7a14a";
var phone = "+16626667521";
const client = require("twilio")(sid, auth_token);
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
const sms = require("fast-two-sms");
const { forEach } = require("lodash");
const {
VerificationCheckInstance,
} = require("twilio/lib/rest/verify/v2/service/verificationCheck");
require("dotenv").config();
const userSchema = new mongoose.Schema({
name: String,
email: String,
phone: String,
age: Number,
address: String,
state: String,
username: String,
password: String,
});
const User = mongoose.model("User", userSchema);
app.set("view engine", "ejs");
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/hackathonDB");
app.get("/", function (req, res) {
User.find({}, function (err, doc) {
console.log(doc);
});
res.render("login");
});
app.get("/registration", function (req, res) {
res.render("registration");
});
app.get("/home", function (req, res) {
res.render("home");
});
app.get("/messaging", function (req, res) {
res.render("messaging");
});
app.post("/", function (req, res) {
const username = req.body.username;
const password = req.body.password;
console.log(req.body);
User.findOne({ username: username }, function (err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
console.log(foundUser);
if (foundUser.password === password) {
res.render("messaging");
}
}
}
});
});
app.post("/messaging", async function (req, res) {
const number = req.body.phone;
const message = req.body.message;
const send_all = req.body.sendAllCheck;
console.log(req.body);
if (send_all === "on") {
User.find({}, function (err, docs) {
docs.forEach(function (doc) {
client.messages
.create({
body: message,
from: phone,
to: "+91" + doc.phone,
})
.then((message) => console.log(message.sid));
});
});
} else {
client.messages
.create({
body: message,
from: phone,
to: number,
})
.then((message) => console.log(message.sid));
}
res.render("messaging");
});
app.post("/registration", function (req, res) {
const name = req.body.name;
const email = req.body.email;
const username = req.body.username;
const password = req.body.password;
const address = req.body.address;
const state = req.body.state;
const user = new User({
name: name,
email: email,
username: username,
phone: req.body.number[1],
age: req.body.number[0],
address: address,
state: state,
password: password,
});
user.save(function (err) {
if (!err) {
res.render("login");
}
});
});
let port = process.env.PORT;
if (port == null || port == "") {
port = "3000";
}
app.listen(port, function () {
console.log("Server has started sucessfully");
});