-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
184 lines (149 loc) · 4.41 KB
/
Copy pathapp.js
File metadata and controls
184 lines (149 loc) · 4.41 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
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
//jshint esversion:6
const express = require("express");
const mongoose = require('mongoose');
const bodyparser=require('body-parser');
const nodemailer=require('nodemailer');
const path=require('path');
const ejs = require("ejs");
const _ = require("lodash");
const homeStartingContent ="";
const aboutContent ="";
const contactContent ="";
const app = express();
app.set('view engine', 'ejs');
app.use(express.urlencoded({extended: true}));
app.use(express.static('public'));
//mongoose.connect("mongodb://localhost:27017/blogDB", {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.connect("mongodb+srv://admin-arunkb:arunkb15@posts.pu1cc.mongodb.net/blog-posts", {useNewUrlParser: true});
const postSchema = {
title: String,
content: String
};
const Post = mongoose.model("Post", postSchema);
// let posts = [];
app.get('/',function(req,res){
res.render('signup');
});
var email;
var otp = Math.random();
otp = otp * 1000000;
otp = parseInt(otp);
console.log(otp);
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 25,
secure: true,
service : 'Gmail',
auth: {
user: 'oscms5609@gmail.com',
pass: '@Placement2022',
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
},
});
app.post('/send',function(req,res){
email=req.body.email;
// send mail with defined transport object
var mailOptions={
to: req.body.email,
subject: "Otp for registration is: ",
html: "<h3>OTP for account verification is </h3>" + "<h1 style='font-weight:bold;'>" + otp +"</h1>"
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
res.render('otp',{msg:"otp has been sent"});
});
});
app.post('/verify',function(req,res){
if(req.body.otp==otp){
app.get("/home", function(req, res){
const pageCount = 2;
Post.find({}, function(err, posts){
let page=Object.keys(posts).length;
if (!page) { page = 1;}
if (page > pageCount) {
page = pageCount
}
//Below code is for pagination(not complete code only limited page we can see using this code we need to modify it as well)
// res.render("home", {
// homeContent: homeStartingContent,
// page: page,
// pageCount: pageCount,
// posts: posts.slice(page * 2 - 2, page * 2)
// });
res.render("home", {
homeContent: homeStartingContent,
page: page,
pageCount: pageCount,
posts: posts
});
});
});
res.redirect("/home");
app.get("/compose", function(req, res){
res.render("compose");
});
app.post("/compose", function(req, res){
const post= new Post({
title: req.body.postTitle,
content: req.body.postBody,
});
post.save(function(err){
if(!err){
res.redirect("/home");
}
});
});
app.get("/posts/:postId", function(req, res){
const requestedId = req.params.postId;
Post.findOne({_id: requestedId},function(err, post){
res.render("post", {
title: post.title,
content: post.content,
});
});
});
app.get("/about", function(req, res){
res.render("about", {about: aboutContent})
});
app.get("/contact", function(req, res){
res.render("contact", {contact: contactContent})
});
app.get("/posts", function(req, res){
res.redirect("/home");
});
}
else{
res.render('otp', {
msg : 'otp is incorrect'
});
}
});
app.post('/resend',function(req,res){
var mailOptions={
to: email,
subject: "Otp for registration is: ",
html: "<h3>OTP for account verification is </h3>" + "<h1 style='font-weight:bold;'>" + otp +"</h1>"
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
res.render('otp',{msg:"otp has been sent"});
});
});
let port=process.env.PORT;
if(port == null || port == ""){
port = 3000;
}
app.listen(port, function() {
console.log("Server started on port 3000");
});