-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
376 lines (292 loc) · 9.64 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//Headers
// http://localhost:3000/
// https://git.heroku.com/shrouded-harbor-77455.git
// https://shrouded-harbor-77455.herokuapp.com/
//jshint esversion: 6
const express = require("express");
const bodyParser = require("body-parser");
const https = require("https");
const request = require("request");
const path = require("path");
//Image upload
const multer = require("multer");
var store = require("multer");
//var tore = require("multer");
const fs = require('fs');
//const gs = require('fs');
//Image upload
const _ = require("lodash");
const app = express();
const {Article} = require(__dirname + "/mongoose.js");
const {Image} = require(__dirname + "/mongoose.js");
const {Member} = require(__dirname + "/mongoose.js");
const {User} = require(__dirname + "/mongoose.js");
const {ArkaEvent} = require(__dirname + "/mongoose.js");
const {ArkaActivity} = require(__dirname + "/mongoose.js");
const {ArkaNews} = require(__dirname + "/mongoose.js");
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true,}));
//Image upload
var storage = multer.diskStorage({
destination: function(req,file,callback){
callback(null,"uploads");
},
filename: function(req,file,callback){
var ext = file.originalname.substr(file.originalname.lastIndexOf("."));
callback(null,file.fieldname+"-"+Date.now()+ext);
}
});
store = multer({storage:storage});
//Constant Delaration
const about = "The Astronomy Club of IIT(ISM) Dhanbad is a platform for all those who have ever looked into the night sky, awe at its magnificence and stuck with its mysteries."+
" This website is an interactive platform for all those minds which have ever rose the question how, when, where and why on looking into vastness and grandeur of the cosmos with a vision of solving the mysteries lurking in it and increasing public awareness and understanding of science and astronomy.";
const customListName = "abcd";
const storedtitle = "abcd";
const ArkaFacts = "ArkaFacts";
const ArkaEvents = "ArkaEvents";
const ArkaActivites = "ArkaActivites";
const Arkanews = "ArkaNews";
//Get,Post functions
app.get("/",function(req,res){
Image.find({},function(error,foundImages){
Article.find({},function(err,foundArticle){
ArkaEvent.find({},function(err,foundevents){
ArkaActivity.find({},function(err,foundactivities){
ArkaNews.find({},function(err,foundnews){
res.render("home",
{articlelist:foundArticle,
imageslist:foundImages,
aboutDescription: about,
arkaevent:foundevents,
arkaactivity:foundactivities,
arkanews:foundnews
});
});
});
});
});
});
});
//
app.get("/members", function (req, res) {
Image.find({},function(error,foundImages){
Member.find({},function(err,foundMembers){
res.render("members", {memberlist:foundMembers,imageslist:foundImages});
});
});
});
app.post("/members", function (req, res) {
});
app.get("/memberscreate", function (req, res) {
res.render("memberscreate");
});
app.post("/memberscreate",function(req,res){
//console.log(req.body);
Member.findOne({name:req.body.name },function(err,foundMember){
if(!err){
if(!foundMember){
const member = new Member({
name: req.body.name,
year: req.body. year,
branch: req.body.branch,
links:{
phone: req.body.phone,
github: req.body.github,
linkedln: req.body.linkedln,
email: req.body.email
}
});
member.save();
res.redirect("/members");
}
}
});
});
app.get("/articles", function (req, res) {
Image.find({},function(error,foundImages){
Article.find({},function(err,foundArticle){
res.render("articles", {articlelist:foundArticle,imageslist:foundImages});
});
});
});
app.get("/arkaevent", function (req, res) {
Image.find({},function(error,foundImages){
ArkaEvent.find({},function(err,foundevents){
res.render("arkaevent", {eventlist:foundevents,imageslist:foundImages});
});
});
});
app.get("/arkanews", function (req, res) {
Image.find({},function(error,foundImages){
ArkaNews.find({},function(err,foundnews){
res.render("arkanews", {newslist:foundnews,imageslist:foundImages});
});
});
});
app.get("/arkaactivity", function (req, res) {
Image.find({},function(error,foundImages){
ArkaActivity.find({},function(err,foundactivity){
res.render("arkaactivity", {activitylist:foundactivity,imageslist:foundImages});
});
});
});
app.get("/admin", function (req, res) {
res.render("admin");
});
app.get("/articlecreate",function(req,res){
res.render("articlecreate");
});
app.post("/articlecreate",function(req,res){
// console.log(req.body);
if (req.body.type == ArkaFacts){
Article.findOne({title:req.body.articlename },function(err,foundArticle){
if(!err){
if(!foundArticle){
const article = new Article({
type: req.body.type,
title: req.body.articlename,
descrition: req.body.description,
credits:{
forcaption: req.body.caption,
forposter: req.body.poster
},
hashtags: req.body.hashtags,
date: req.body.date
});
article.save();
res.redirect("/articles");
}
}
});
}
if (req.body.type == ArkaActivites){
ArkaActivity.findOne({title:req.body.articlename },function(err,foundArticle){
if(!err){
if(!foundArticle){
const arkaactivity = new ArkaActivity({
type: req.body.type,
title: req.body.articlename,
descrition: req.body.description,
hashtags: req.body.hashtags,
date: req.body.date
});
arkaactivity.save();
//res.redirect("/arkaevents");
}
}
});
}
if (req.body.type == ArkaEvents){
ArkaEvent.findOne({title:req.body.articlename },function(err,foundArticle){
if(!err){
if(!foundArticle){
const arkaevent = new ArkaEvent({
type: req.body.type,
title: req.body.articlename,
descrition: req.body.description,
hashtags: req.body.hashtags,
date: req.body.date
});
arkaevent.save();
//res.redirect("/arkaevents");
}
}
});
}
if (req.body.type == Arkanews){
ArkaNews.findOne({title:req.body.articlename },function(err,foundArticle){
if(!err){
if(!foundArticle){
const arkanews = new ArkaNews({
type: req.body.type,
title: req.body.articlename,
descrition: req.body.description,
hashtags: req.body.hashtags,
date: req.body.date
});
arkanews.save();
//res.redirect("/arkaevents");
}
}
});
}
});
//Image upload
app.get("/uploadmultiple",store.array("images",6),function(req,res,next){
console.log(req.body)
});
app.post("/uploadmultiple",store.array("images",6),function(req,res,next){
const files = req.files;
if(!files){
const error = new Error("Please choose files");
error.httpStatusCode = 400;
return next(error);
}
//convert images into base64 encoding
let imgArray = files.map((file) =>{
let img = fs.readFileSync(file.path);
return encode_image = img.toString('base64');
});
let result = imgArray.map((src,index)=>{
//Create object to store data in collection
let finalimg = new Image({
type: req.body.type,
title: req.body.articlename,
filename: files[index].originalname,
contentType : files[index].mimetype,
imageBase64: src
});
return finalimg.save().then(function(){
return {msg : files[index].originalname + " Uploaded Successfuly"};
}).catch(function(error){
if(error){
if(error.name === "MongoError" && error.code === 11000){
return Promise.reject({
error: "Duplicate " + files[index].originalname +"File Already exists!"
});
}
return Promise.reject({
error: error.message|| "Cannot Upload " + files[index].originalname +"something missing"
})
}
})
});
Promise.all(result).then(msg => {
//res.json(msg);
console.log(msg);
res.redirect("/articlecreate");
})
.catch(err =>{
console.log(err);
});
//console.log(result);
});
//Image upload
//Express Route Parameters
app.get("/:customListName" , function(req,res){
//console.log(req);
const customListName = _.lowerCase(req.params.customListName);
Image.find({},function(error,foundImages){
Article.find({},function(err,foundArticle){
foundArticle.forEach(function(post){
const storedtitle = _.lowerCase(post.title);
if(storedtitle == customListName){
res.render("articlesdetails", {atitle: post.title ,
adescription: post.descrition,ahashtag: post.hashtags,
aforcaption: post.credits.forcaption,
aforposter: post.credits.forposter,imageslist:foundImages
});
}
});
})
});
});
app.post("/:customListName" , function(req,res){
const customListName = _.lowerCase(req.params.customListName);
res.redirect("/"+customListName);
});
//Get,Post functions
app.listen(process.env.PORT || 3000, function () {
console.log("server started on port 3000");
});