-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
206 lines (156 loc) · 4.97 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
console.log("server is starting");
var fs = require('fs');
var data = fs.readFileSync('score.json');
var score = JSON.parse(data);
var afinnFile = fs.readFileSync('afinn.json');
var afinnData = JSON.parse(afinnFile);
var pingLimit = 0;
function ping(){
setTimeout(() => {
console.log("pinging requip/herokuapp.com");
fetch("http://requip.herokuapp.com/ping").then(response => {
return response.json();
}).then(json => {
return JSON.stringify(json);
});
}, 1);
}
const { response, json, request } = require('express');
var express = require('express');
var cors = require('cors')
const fetch = require("node-fetch");
const { stringify } = require('querystring');
const { finished } = require('stream');
const { SSL_OP_EPHEMERAL_RSA } = require('constants');
var app = express();
// var server = app.listen(8000, listening);
var server = app.listen(process.env.PORT || 3000, listening);
function listening() {
console.log("listening...");
/* disabling ping , to enable uncomment ping call from this block as well as in get_ping function */
//ping();
}
app.use(cors())
var corsOptions = {
origin: ['http://127.0.0.1:5500','http://127.0.0.1:5500','http://127.0.0.1:5501',"https://requip.herokuapp.com/"],
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
var postCorsOptions = {
"origin": ['http://127.0.0.1:5500','http://127.0.0.1:5501'],
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204,
"Content-Type":"appllication/json"
}
app.use(express.static('website'));
app.use(express.json())
app.options('/data', cors(postCorsOptions)) // enable pre-flight
app.post("/data", cors(postCorsOptions) , (request, response) => {
var file = request.body;
console.log(file.value);
console.log("post request recieved in server");
reply = {
'status': 'success',
'data_recieved' : {
'key': request.body.key,
'value': request.body.value
}
}
score[request.body.key]=request.body.value;
try{
fs.writeFile('score.json', JSON.stringify(score, null, 2), err => {
console.log("writing into the file from post request");
});
} catch(error){
console.error(error);
}
response.send(reply);
});
app.get("/ping",cors(corsOptions), get_ping);
app.get("/search/:flower/:num?", get_sunflower);
app.get("/all",cors(corsOptions) ,sendall);
app.get("/add/:flower/:num", addNew);
app.get("/delete/:flower", deleteEntry);
function get_ping(request, response){
pingLimit+=1;
if(pingLimit>1){
pingLimit-=1;
response.send({
"received":"true",
"throttling":"true"
});
return;
}
console.log("recieved ping from requip.herokuapp.com");
var reply = {
"ping":"recieved",
"active":"true"
};
function ping(){
setTimeout(async () => {
console.log("pinging requip/herokuapp.com");
await fetch("http://requip.herokuapp.com/ping").then(response => {
return response.json();
}).then(json => {
pingLimit-=1;
return JSON.stringify(json);
});
}, 1000*60*10);
}
// ping();
response.send(reply);
}
function deleteEntry(request, response) {
var data = request.params;
var flower = data.flower;
if(score[flower]!=undefined)
{
delete score[flower];
fs.writeFile('score.json', JSON.stringify(score, null, 2), finished);
function finished(err) {
console.log(`deleting ${flower} from database`);
}
}
else
console.log(`element with key "${flower}" not in database`);
response.send(score);
}
function addNew(request, response) {
var data = request.params;
var flower = data.flower;
var num = data.num;
var reply = {
"word": flower,
"status": "successful"
};
score[flower] = Number(num);
try {
fs.writeFile('score.json' , JSON.stringify(score, null, 2), err => {
console.log("writing into the file\n");
});
} catch (error) {
response.send("error while writing into file");
}
response.send(reply);
}
function sendall(request, response) {
var head = response['req']['headers']['origin'];
console.log("===========================================================================");
console.log(head);
console.log("===========================================================================");
reply = {
"score": score,
"afinn data":afinnData
}
response.send(reply);
}
function get_sunflower(request, response) {
var data=request.params;
var num =data.num ;
if(!num)
num=1;
var reply='';
for(var i=0;i<num;i++)
reply+=`<h1>I love ${data.flower} ${i}\n</h1>`;
response.send(reply);
}