-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
98 lines (71 loc) · 2.78 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
//To load environment variables
//Since code will be up on GitHub, we are storing mongoDB credentials as .env var
require("dotenv").config();
//NPM packages ===============================
let express = require("express");
let app = express();
let bodyParser = require("body-parser");
//``````````````````````````````````````````````
app.use(bodyParser.urlencoded({extended:true}));
//``````````````````````````````````````````````
let mongoose = require("mongoose");
let passport = require("passport");
let LocalStrategy = require("passport-local");
//--------------------------------------------
// _requiring and using express session_
app.use(require("express-session")({
secret: "Harish please cum",
resave: false,
saveUninitialized: false
}));
//=============================================
//Mongoose model created in /models/userModel
let User = require("./models/userModel")
//Configuring passport ===================
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// =======================================
app.set("view engine", "ejs");
console.log(__dirname +"/src");
app.use("/src",express.static("src"));
app.use("/src",express.static("src"));
app.use("/css",express.static("css"));
app.use("/assets",express.static("assets"));
const routes = require("./routes/main");
// importing routes for viewing and submitting scores from secure.js
const secureRoutes = require("./routes/secure");
//Using mongoose for the database
//Setting up mongoose connection
// // const uri = process.env.MONGO_CONNECTION_URL;
// mongoose.connect(uri, { useNewUrlParser : true, useCreateIndex: true, useUnifiedTopology: true });
// mongoose.connection.on('error', (error) => {
// console.log(error);
// process.exit(1);
// });
mongoose.connection.on('connected', function () {
console.log('Connected to Mongo!!!');
});
//**********************************************
//**********************************************
mongoose.connect("mongodb+srv://rish-game_20:[email protected]/test?retryWrites=true&w=majority");
//Refactoring the routes
app.use("/",routes);
app.use("/", secureRoutes);
//Using middleware ****************************
// app.use(function(req,res){
//
// res.send("Not connected to any routes");
// })
// Send error message in case there is any error
app.use(function (error,req,res,next){
res.send("Error");
console.log(error.message)
})
//localhost:3000 is being used for testing purpose
//Harish knows the port of the deployment program
app.listen(3000,function () {
console.log("Server working.");
});