-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
291 lines (207 loc) · 6.07 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//jshint esversion: 6
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
//const request = require ("request");//not used in the end
const https= require("https");
//const nodemailer = require('nodemailer');//not used in the end
//const $= require("jquery"); //not used in the end
//creation of new espress object
const app= express();
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
//news data copy, pageId
let newsCopy=[];
let NewsContent=[];
let pageId=0;
let numberResults=0;
let pageNumber=0;
let screenWidth=0;
//news api
//const OptionPath1="/api/search/NewsSearchAPI?q=";//removed
const OptionPath1="/youtube-search/?q=";
const OptionPath2="";
//const OptionPath2="&pageNumber=1&pageSize=50&autoCorrect=true&fromPublishedDate=null&toPublishedDate=null";
const options = {
"method": "GET",
//"hostname": "contextualwebsearch-websearch-v1.p.rapidapi.com",
hostname: 'youtube-search-results.p.rapidapi.com',
"port": null,
"path": "",
"headers": {
"X-RapidAPI-Key": process.env.API_KEY,
// "X-RapidAPI-Host": "contextualwebsearch-websearch-v1.p.rapidapi.com",//removed
'X-RapidAPI-Host': 'youtube-search-results.p.rapidapi.com',
"useQueryString": true
}
};
//about me page
app.get("/about-me",function(req,res){
res.render("about");
})
//contact me page
app.get("/contact-me",function(req,res){
res.render("contact");
})
//message sent successfully display message-sent page
app.post("/message-sent",function(req,res){
//just to see the request's body
//console.log(req.body);
// Define message object to send in database(mongodb)
var message = {
email: req.body.email, // Sender ID
firstname: req.body.firstname,
lastname: req.body.lastname,
content:req.body.subject
};
res.render("message-sent");
})
//request to aiqualityApp page
app.get("/airqualityApp",function(req,res){
res.render("airqualityApp");
})
//request to projects page
app.get("/projects",function(req,res){
res.render("projects");
})
//get request to the root
app.get("/",function(req,res){
res.sendFile(__dirname+"/index.html");
});
//post(search form) to news page
app.post("/news",function(request,response){
//catch entered text and update api options
let body=request.body.searchText;
body=body.toLowerCase();
body=encodeURIComponent(body.trim());
//console.log(body);
options["path"]=OptionPath1+body+OptionPath2;
const req = https.request(options, function (res) {
let chunks = "";
res.on("data", function (chunk) {
chunks += chunk;
});
res.on("end", function () {
let datas=JSON.parse(chunks);
//console.log(datas)
datas=datas["videos"];
//console.log(datas);
numberResults=datas.length; //between 0 to 50 for a given research
console.log(numberResults)
let news=[];
for(let i=0; i<numberResults; i++){
if(datas[i]["id"]!="" && datas[i]["thumbnail"]!=""){
news.push(
[ datas[i]["thumbnail"],
datas[i]["title"] ,
datas[i]["link"],
datas[i]["description"],
datas[i]["views"]
]);
}
}//end for bracket
//newsCopy=news;
//NewsContent=news.slice(0,9);
//NewsContent=news;
//console.log(news);
/*pageNumber=Math.floor(numberResults/10)+1;
pageId=1;
response.render('news',{data:NewsContent, numberResults: pageNumber, pageId:1, width:screenWidth});*/
response.render('news',{data:news, numberResults: pageNumber, pageId:1, width:screenWidth});
});
res.on("error", (err) => {
console.log("Error: " + err.message);
res.render("ApiError");
});
});
req.end();
});
//request to news-content page
app.get("/news-content/:contentID",function(req,res){
let id=req.params.contentID;
//console.log(id);
//console.log(typeof(id));
res.render("news-content",{data:NewsContent,id:id});
})
//to manage the other content pages
app.get("/news/:ID",function(req,res){
let id=req.params.ID;
id=parseInt(id);
if(id>30){
screenWidth=id; //screen width in px
res.render("news",{data:["welcome"]});
}
else if(id>=1 && id<=30){
pageId=id;
}
//previous page
else if (id===0) {
if(pageId>1){
pageId--;
}
}
//Next page
else{
pageId++;
if(pageId>=pageNumber){
pageId=1;
}
}
//render page
if(newsCopy.length>=pageId*10 -1){
res.render("news",{data:newsCopy.slice((pageId-1)*10,(pageId*10)), numberResults: pageNumber, pageId:pageId, width:screenWidth });
NewsContent=newsCopy.slice((pageId-1)*10,(pageId*10));
}
else{
res.render("news",{data:newsCopy.slice((pageId-1)*10,newsCopy.length), numberResults: pageNumber, pageId:pageId, width:screenWidth});
NewsContent=newsCopy.slice((pageId-1)*10,newsCopy.length);
}
})
/*request to news page
app.get("/news",function(req,res){
res.render("news",{data:["welcome"]});
})*/
//send form data : email, last name, first name, message to mongodb
//connection to mongo db locally
mongoose.connect("mongodb+srv://"+process.env.MONGODB_USERNAME+":"+process.env.MONGODB_PASSWORD+"@contactmessage.3sihltd.mongodb.net/messageDB",function(err){
if(err){
console.log("connection to db failed");
console.log(err);
} else{
console.log("connection to db succeed");
}
});
const messageSchema = new mongoose.Schema ({
email: String,
firstname: String,
lastname: String,
message: String
});
const Message = mongoose.model("Message", messageSchema);
app.post("/contact",function(req,res){
const contactMessage = new Message ({
email:req.body.email,
firstname:req.body.firstname,
lastname:req.body.lastname,
message:req.body.subject
});
contactMessage.save(function(err){
if(err){
res.send("something goes wrong, please try again!");
}
else{
res.render("message-sent");
}
}); //to insert the message in the DB
})//end app.post
//for both : heroku and localhost
var port = process.env.PORT;
if(port==null || port==""){
port=3000;
}
app.listen(port, function() {
//console.log("Server has started successfully on port "+ port);
console.log("Server has started successfully");
});