-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
174 lines (145 loc) · 4.14 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
const { urlencoded } = require('express');
const express = require('express');
const ejs = require('ejs');
const mongoose = require('mongoose');
const _ = require('lodash');
const session = require('express-session');
const passport = require('passport');
const passportLocalMongoose = require('passport-local-mongoose');
const opn = require('opn');
const app = express()
app.use(urlencoded({ extended: true }))
app.set('view engine', 'ejs')
app.use(express.static("public"))
app.use(session({
secret: "our long secret",
resave: false,
saveUninitialized: false,
}))
app.use(passport.initialize());
app.use(passport.session())
mongoose.connect("mongodb://localhost:27017/factsDB", { useUnifiedTopology: true, useNewUrlParser: true });
mongoose.set('useCreateIndex', true);
const AdminSchema = new mongoose.Schema({
username: String,
password: String
})
const FactsSchema = {
title: String,
content: String
}
AdminSchema.plugin(passportLocalMongoose);
const Admin = mongoose.model("admin", AdminSchema);
const Facts = mongoose.model("post", FactsSchema);
passport.use(Admin.createStrategy());
passport.serializeUser(Admin.serializeUser());
passport.deserializeUser(Admin.deserializeUser());
app.get('/', (req, res) => {
Facts.find({}, (err, post) => {
if (err) {
console.log(err);
}
else {
res.render("home", { Data: post })
}
})
})
app.get("/post/:id", (req, res) => {
let postid = req.params.id;
Facts.findById(postid, (err, post) => {
if (err) {
console.log(err);
}
else {
res.render("post", { Title: post.title, Body: post.content })
}
})
})
app.get("/admin", (req, res) => {
if (req.isAuthenticated()) {
Facts.find({}, (err, post) => {
if (err) {
console.log(err);
}
else {
res.render("admin", { Data: post })
}
})
}
else {
res.redirect("/admin/login")
}
})
app.get("/admin/login", (req, res) => {
res.render("login")
})
app.post("/admin/login", (req, res) => {
Admin.findOne({ username: req.body.username }, function (err, uname) {
if (uname) {
const user = new Admin({
username: req.body.username,
password: req.body.password
});
passport.authenticate("local", function (err, user) {
if (err) {
console.log(err);
} else {
if (user) {
req.login(user, function () {
res.redirect("/admin")
})
} else {
res.redirect("/admin/login")
}
}
})(req, res);
} else {
res.redirect("/")
}
})
})
app.post("/admin", (req, res) => {
let data = req.body
Facts.insertMany({
title: data.Title,
content: data.Content
})
res.redirect("/admin")
})
app.get("/admin/register", (req, res) => {
if (req.isAuthenticated()) {
res.render("create")
} else {
// res.redirect("/")
res.render("create")
}
})
app.post("/admin/register", function (req, res) {
Admin.register({ username: req.body.username }, req.body.password, function (err, user) {
if (err) {
console.log(err);
res.redirect("/admin/register")
} else {
res.redirect("/admin")
}
})
})
app.post('/admin/logout', (req, res) => {
req.logout();
res.redirect("/")
})
app.post("/delete", (req, res) => {
let id = req.body.id.trim();
Facts.deleteOne({ _id: id }, function (err) {
if (err) {
console.log(err);
}
else {
res.redirect("/admin")
}
})
})
app.listen(3000, () => {
console.log("server started at localhost:3000")
opn("http://localhost:3000/")
})