-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
153 lines (130 loc) · 4.63 KB
/
app.js
File metadata and controls
153 lines (130 loc) · 4.63 KB
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
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const lodash=require("lodash");
const mongoose=require("mongoose");
const homeStartingContent = "This is a competitive programming journal where group of cp enthusiasts brings good article of certain cp topics at one place, so that we can learn effectively and easily.We try to include all important data structures and algorithms needed to master cp. Please use this site only to read and if you want to contribute some article to this website then first contact us with info given in contact us page. I hope this website will prove resoursefull to you all. Happy learning!";
const aboutContent = "Presently, this site is maintained by Ankit. I initially designed this site for myself so that i can bring all important tricks and ideas related to competitive programmming that i learn from different sites and blogs at one place for future revision. Later i decided to give to some of my close friends so that we all can learn effectively and easily.Please use this site only to read and if you want to contribute some article to this website then first contact us with info given in contact us page.";
const contactContent = "You can contact us at codeforces my id is riddle279 . You can also contact us with mail my email is sahooankit12@gmail.com";
const app = express();
app.set('view engine', 'ejs');
app.use(express.urlencoded({extended: true}));
app.use(express.static("public"));
mongoose.connect("mongodb+srv://admin-ankit:123@cluster0.f47j9.mongodb.net/blogDB?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
}).then(() => {
console.log('MongoDB connected!!');
}).catch(err => {
console.log('Failed to connect to MongoDB', err);
});
const opts = {
// Make Mongoose use Unix time (seconds since Jan 1, 1970)
timestamps: { currentTime: () => Math.floor(Date.now() / 1000) },
};
// Declaring Schema for posts.
const bSchema=mongoose.Schema({
btitle:String,
bbody:String,
},opts);
const Bpost=mongoose.model("Bpost",bSchema);
// To render "About Us" page with aboutContent declared above.
app.get("/about",function(req,res){
res.render("about",{aboutcontent:aboutContent});
});
// To render "Contact Us" page with contactContent declared above.
app.get("/contact",function(req,res){
res.render("contact",{contactcontent:contactContent});
});
// To render "Compose" page.
app.get("/compose",function(req,res){
res.render("compose");
});
// To render "Home" page with homeStartingContent declared above and all posts in sorted order(Most recent post is at top).
app.get("/",function(req,res){
Bpost.find({},null,{sort: {createdAt: -1}},function(err,postitem){
if(err){
console.log(err);
}else {
res.render("home",{homecontent:homeStartingContent,
addcontent:postitem});
}
});
});
// To render fullPagePost for each post.
app.get("/posts/:topic",function(req,res){
let myvar=req.params.topic;
Bpost.findOne({_id:myvar},function(err,post){
if(err){
console.log(err);
}else {
res.render("post",{fullPagePost:post});
}
});
});
// Logic to compose a post, when publish button is clicked.
app.post("/compose",function(req,res){
const bpost=new Bpost({
btitle:req.body.postTitle,
bbody:req.body.postBody,
});
bpost.save(function(err){
if(!err){
res.redirect("/");
}
});
});
// Logic to update a post.
app.post("/updcompose",function(req,res){
const pid=req.body.postid;
const ptitle=req.body.postTitle;
const pbody=req.body.postBody;
Bpost.deleteOne({_id:pid},function(err){
if(err){
console.log(err);
}else{
const bpost1=new Bpost({
btitle:ptitle,
bbody:pbody,
});
bpost1.save(function(err){
if(!err){
res.redirect("/");
}
});
}
});
});
// Logic to delete a post.
app.post("/delete",function(req,res){
const posttodelete=req.body.dpost;
Bpost.deleteOne({_id:posttodelete},function(err){
if(err){
console.log(err);
}else{
res.redirect("/");
}
});
});
// Wrapper to update a post.
app.post("/update",function(req,res){
let myupdpost=req.body.updpost;
Bpost.findOne({_id:myupdpost},function(err,post){
if(err){
console.log(err);
}else {
res.render("updcompose",{blogppost:post});
}
});
});
// Declaring the port on which the app will listen.
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log("Server started succesfully");
});