-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
131 lines (110 loc) · 3.54 KB
/
Copy pathapp.js
File metadata and controls
131 lines (110 loc) · 3.54 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
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request")
const https = require("https");
const { response } = require('express');
const _ = require('lodash');
const mongoose = require('mongoose');
mongoose.connect("mongodb+srv://Use-your-own-API-here", {useNewUrlParser: true, useUnifiedTopology: true });
const postSchema = {
title: String,
body: String,
writer: String,
timeAndDate: String
};
const Post = mongoose.model("Post", postSchema);
const app = express();
// Auth0 User Authentication
const { auth } = require('express-openid-connect');
const { requiresAuth } = require('express-openid-connect');
const config = {
authRequired: false,
auth0Logout: true,
secret: 'Use-your-own-code-here',
baseURL: 'http://blogora.herokuapp.com',
clientID: 'Use-your-own-ID-here',
issuerBaseURL: 'Use-your-URL-code-here'
};
// auth router attaches /login, /logout, and /callback routes to the baseURL
app.use(auth(config));
// req.isAuthenticated is provided from the auth router
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/",function(req,res){
Post.find({}, function(err, posts){
res.render("home.ejs",{posts : posts });
});
})
app.get("/about",function(req,res){
res.render("about.ejs")
})
app.get("/compose",requiresAuth(),function(req,res){
res.render("compose.ejs")
})
app.get("/subscribe",function(req,res){
res.render("subscribe.ejs")
})
app.get("/post/:postId",requiresAuth(),function(req,res){
const requestedPostId = req.params.postId;
Post.findOne({_id: requestedPostId}, function(err, post){
if (err){
return res.status(500).send({ msg: err.message });
res.redirect("/");
}
else{
res.render("post", {title : post.title , body : post.body, timeAndDate:post.timeAndDate, writer : post.writer });
}
});
})
app.post("/compose",function(req,res){
var timeAndDate = new Date().toLocaleString('en-US', { timeZone: 'Asia/Kolkata' })
timeAndDate = timeAndDate.toLocaleString()
const post = new Post({ title : req.body.postTitle, body : req.body.postBody, timeAndDate : timeAndDate, writer : req.body.postWriter });
post.save(function(err){
if (err){
return res.status(500).send({ msg: err.message });
res.redirect("/");
}
else{
res.redirect("/");
}
});
// res.redirect("/")
})
app.post("/subscribe",function(req,res){
const name = req.body.name;
const email = req.body.email;
const data = { members : [ { email_address : email, status : "subscribed",
merge_fields : {
NAME : name,
PASSWORD : name
}}]
}
const jsonData = JSON.stringify(data);
const url = 'Use-your-own-URL-here'
const options = { method : "POST", auth : "Use-your-own-token-here" }
const request = https.request(url,options,function(response){
response.on("data",function(data){
const newData = JSON.parse(data)
console.log(newData)
if(response.statusCode == 200 && newData.error_count == 0 ){
res.render("success.ejs")
}
else{
if(newData.errors[0].error_code == 'ERROR_CONTACT_EXISTS'){
res.render("already.ejs")
}
else{
res.render("failure.ejs")
}
}
})
})
request.write(jsonData)
request.end()
})
app.listen(process.env.PORT || 3000,function(){
console.log("Server running on port 3000")
})