-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
179 lines (143 loc) · 3.71 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
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const _ = require("lodash");
const mongoose = require("mongoose");
const blogx = require("./views/blogs.js");
const showdown = require('showdown');
var converter = new showdown.Converter();
require('dotenv').config();
const port = process.env.PORT || 3000;
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// Authentication using Auth0
const { auth,requiresAuth } = require('express-openid-connect');
const config = {
authRequired: false,
auth0Logout: true,
secret: process.env.SECRET,
baseURL: process.env.BASE_URL,
clientID: process.env.CLIENT_ID,
issuerBaseURL: process.env.ISSUER_BASE_URL
};
app.use(auth(config));
// Connection with mongodb database
mongoose.connect(process.env.MONGODB_REMOTE, { useNewUrlParser: true }).then(()=>{
console.log("mongodb connected...")
})
const postSchema = {
title: String,
date : String,
time : Number,
content: String
};
const Post = mongoose.model("Post", postSchema);
let category = [];
let hashTag;
let todaysDate;
const date = new Date();
todaysDate = date.toDateString()
app.use('/posts', blogx)
app.get("/", (req, res) => {
res.render("index");
})
app.get('/compose', requiresAuth(), (req, res) => {
res.render('compose');
});
app.get('/profile', requiresAuth(), (req, res) => {
let user = req.oidc.user;
res.render('profile',
{image:user.picture,
name:user.name,
nickname:user.nickname,
email:user.email,
givenName:user.given_name,
familyName:user.family_name,
language:user.locale,
verificationStatus:user.email_verified});
})
app.post('/addCategory', requiresAuth(), (req, res) => {
res.send( res.redirect("/login"));
});
app.get("/categories", (req, res) => {
category = ["Education", "Fashion", "Art", "Photography", "Fun", "Information", "Music"]
res.render("categories", { category: category });
})
app.get("/blogs", (req, res) => {
Post.find({}, function (err, posts) {
res.render("blogs", {
posts: posts
});
})
})
app.get("/about", (req, res) => {
res.render("about", { aboutContent: aboutContent });
});
app.get("/contact", (req, res) => {
res.render("contact", {});
});
app.get("/compose", (req, res) => {
res.render("compose");
})
app.get("/reviews", (req, res) => {
Post.find({}, function (err, posts) {
res.render("reviews", {
posts: posts
});
})
})
app.post("/addCategory", (req, res) => {
hashTag = {
tag: req.body.postTitle
};
category.push(hashTag);
res.redirect("/categories")
})
app.post("/compose", (req, res) => {
let content = req.body.postBody;
let readT = readTime(wordCount(content));
let html = converter.makeHtml(content);
const post = new Post({
title: req.body.postTitle,
date:todaysDate,
time:readT,
content: html
});
if(wordCount(content)!=0){
post.save(err=> {
if (!err) {
res.redirect("/blogs");
}else{
console.log(err);
}
});
}
});
app.get("/post/:postId", (req, res) => {
const requestedPostId = req.params.postId;
Post.findOne({ _id: requestedPostId }, function (err, post) {
res.render("post", {
title: post.title,
date: post.date,
time: post.time,
content: post.content
});
});
});
function wordCount(str) {
str = str.replace(/(^\s*)|(\s*$)/gi, "");
str = str.replace(/[ ]{2,}/gi, " ");
str = str.replace(/\n /, "\n");
count = str.split(' ').length;
return count;
}
function readTime(wordCount){
let readT = wordCount/125;
readT = Math.round(readT);
return readT;
}
app.listen(port, () => {
console.log("Server started on port " + port);
});