-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
121 lines (95 loc) · 2.86 KB
/
server.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
require("dotenv").config();
const express = require('express');
const path = require('path');
const bodyparser = require('body-parser');
var _ = require('lodash');
const mongoose = require("mongoose");
const https = require('https');
const app = express();
const portNum = process.env.portNum || 3000;
app.use(bodyparser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
mongoose.connect(process.env.MONGODB_CONNECT);
const progressSchema = new mongoose.Schema({
Title: {
type: String,
required: true
},
PostDescription: {
type: String,
require: true
}
});
const SignUpDBSchema = new mongoose.Schema({
email_info: {
type: String,
required: true
},
Name: {
type: String,
required: true
},
Message: {
type: String
}
});
const postInfo = mongoose.model("postdetails", progressSchema);
app.get('/', async (req, res) => {
const processDetailsList = await postInfo.find({});
res.render("index", { progressLists: processDetailsList })
})
app.get('/aboutUs', (req, res) => {
res.render('aboutUs');
})
app.get('/contactUs', (req, res) => {
res.render('contactUs');
})
app.get('/AddProgress', (req, res) => {
res.render('addProgress');
})
app.get("/posts/:postName", async (req, res) => {
const postid = _.lowerCase(req.params.postName);
const processDetailsList = await postInfo.find({});
processDetailsList.forEach((post) => {
const titleid = _.lowerCase(post._id);
if (titleid === postid) {
res.render('post', { PostTitle: post.Title, PostDescription: post.PostDescription });
}
});
});
app.post('/AddProgress', async (req, res) => {
const posttitle = req.body.postTitle;
const postdesc = req.body.postdescription;
var progress = new postInfo({
Title: posttitle,
PostDescription: postdesc
});
await progress.save();
res.redirect('/AddProgress');
})
app.post('/contactUs', (req, res) => {
const name = req.body.Name;
const email = req.body.email;
const message = req.body.message;
try {
const usersinfo = mongoose.model("usersInfo", SignUpDBSchema);
const userdetail = new usersinfo({
email_info: email,
Name: name,
Message: message
})
userdetail.save();
res.sendFile(path.join(__dirname, '/public/success.html'))
}
catch (err) {
console.log(err);
res.sendFile(path.join(__dirname, '/public/failure.html'));
}
})
app.post('/failure', (req, res) => {
res.redirect("/contactUs");
})
app.listen(portNum, () => {
console.log(`Successfully connected to port ${portNum} :)`);
})