-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
110 lines (100 loc) · 3.56 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
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const client = require("@mailchimp/mailchimp_marketing");
require("dotenv").config();
client.setConfig({
apiKey: process.env.APIKEY,
server: "us17",
});
//------connecting to mongodb server
mongoose
.connect(process.env.MONGODBURI)
.then(() => {
console.log("successfully connected");
})
.catch((err) => console.log(err));
const MongoStore = require("connect-mongo")(session);
//----------------------------------------------routes
// login route
const login = require("./routes/Login.js");
// register route
const register = require("./routes/Register.js");
//codeEditor route
const codeEditor = require("./routes/codeEditor.js");
//home route
const home = require("./routes/codeHome.js");
// blogs route
const blogs = require("./routes/blogs.js");
// logout route
const logout = require("./routes/logout.js");
// invalid login route
const invalidLogin = require("./routes/wrongPassword");
//user codes
const userCodes = require("./routes/userCodes");
//edit route
const edit = require("./routes/edit");
//delete route
const deleteCode = require("./routes/delete");
//view code route
const viewCode = require("./routes/viewcode");
// -------------------------------------------- end of routes
const app = express();
// middleware ----------------------------------------------------------------------!!
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
//middleware ------------------------------------------------------------------------------------------------
//session store ----------------------------------------not used in here-------------------------------------------------------
const sessionStore = new MongoStore({
mongooseConnection: mongoose.connection,
collection: "sessions",
});
//session store -----------------------------------------------------------------------------------------------
//base routes ----------------------------------------------------------------
app.use(
session({
secret: "pavanrajesh",
resave: false,
saveUninitialized: true,
store: new MongoStore({
mongooseConnection: mongoose.connection,
collection: "session",
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24, // Equals 1 day (1 day * 24 hr/1 day * 60 min/1 hr * 60 sec/1 min * 1000 ms / 1 sec)
},
})
);
app.use(passport.initialize());
app.use(passport.session());
// ------These should be all of the above ones
app.use("/", home);
app.use("/login", login);
app.use("/register", register);
app.use("/letscode", codeEditor);
app.use("/blogs", blogs);
app.use("/logout", logout);
app.use("/wrongPassword", invalidLogin);
app.use("/usercodes", userCodes);
app.use("/edit", edit);
app.use("/delete", deleteCode);
app.use("/view", viewCode);
app.post("/addemail", async (req, res) => {
const response = await client.lists.addListMember("701079c3ce", {
email_address: req.body.email,
status: "subscribed",
});
// console.log(response);
});
//base routes ---------------------------------------------------------------- that means these routes will be preced by the double quoted strings in the above ones
// listening on port
app.listen(process.env.PORT || 3000, (err) => {
if (err) throw err;
console.log("listening on port 3000");
});
//port ----------------------------------------------------------------