-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (103 loc) · 3.12 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
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
require('dotenv').config();
const path = require('path');
const sgMail = require('@sendgrid/mail');
const https = require('https');
const express = require('express');
const app = express();
app.use(express.static('./public'));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.get("/", (req, res)=>{
let index = path.join(__dirname, '/', './public', 'index.html' );
res.sendFile(index);
})
const send1 = (usermail, title, body) => {
const SECRET_KEY = `${process.env.SENDGRID_API_KEY}`;
sgMail.setApiKey(SECRET_KEY);
const mail = String(process.env.MAIL);
const msg = {
to: usermail,
from: mail,
subject: title,
html: `<strong>${body}</strong>`,
};
sgMail
.send(msg)
.then(() => {}, error => {
console.error(error);
if (error.response) {
console.error(error.response.body)
}
});
(async () => {
try {
await sgMail.send(msg);
} catch (error) {
console.error(error);
if (error.response) {
console.error(error.response.body)
}
}
})();
}
let data = "";
let enteredTitle= '';
let enteredBody = '';
let i = 0;
app.post("/", (req, res)=>{
const fetchData = async () => {
const url = "https://hospitals-ecf29-default-rtdb.firebaseio.com/notes.json";
https.get(url, res => {
res.on('data', chunk => {
data +=chunk;
});
res.on('end', () => {
data = JSON.stringify(data);
let ans = JSON.parse(data);
// console.log(typeof ans + " " + ans);
var result = Object.keys(ans).map((key) => [Number(key), ans[key]]);
ans = result.toString('utf8')
console.log(typeof result + " " + ans);
// ans = ans.toString();
// ans = ans[Object.keys(ans)[Object.keys(ans).length - 1]];
// console.log(typeof ans);
// console.log("chunk-> " + data[data.length-1]);
// data = JSON.parse(data);
// var result = Object.keys(data).map((key) => [Number(key), data[key]]);
// result = result[result.length-1][1];
// console.log(result);
// enteredTitle = result.title;
// enteredBody = result.body;
// console.log("title : " + enteredTitle);
// console.log("body : " + enteredBody);
});
res.on('end', () => {
try {
const email = req.body.email;
// send1(email.toString('base64'), enteredTitle.toString('base64'), enteredBody.toString('base64'));
} catch (error) {
console.log(error);
}
});
}).on('error', err => {
console.log(err.message);
});
};
if(req.body.email.toString() < 7){
let notFound = path.join(__dirname, '/', './public', 'notFound.html' );
res.sendFile(notFound);
}else{
let successFile = path.join(__dirname, '/', './public', 'success.html' );
fetchData();
res.sendFile(successFile);
}
});
app.listen(process.env.PORT || 3000, () => {
if (process.env.PORT == undefined) {
console.log("Server is up on the port 3000");
}else{
console.log(`Server is up on port ${process.env.port}`);
}
})