-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
105 lines (87 loc) · 2.49 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
99
100
101
102
103
104
105
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var express = require("express");
var methodOverride = require("method-override");
var path = require("path");
var morgan = require("morgan");
var cors = require("cors");
var multer = require("multer");
var passport = require("passport");
var flash = require("connect-flash");
var session = require("express-session");
var app = express();
var url = "mongodb://localhost:27017/coursehub";
var indexRoutes = require("./routes/index");
var subjectRoutes = require("./routes/subject");
var resourceRoutes = require("./routes/resources");
var userRoutes = require("./routes/user");
var {
deleteAllSubjects,
deleteAllResources,
makeAdmins,
makeSuperAdmins,
deleteAllUsers,
} = require("./helper");
// setting up mongoose connection
mongoose
.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
console.log("Connected to Database");
})
.catch((err) => {
console.log(err.message);
});
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan("dev"));
// Passport config
require("./config/passport")(passport);
app.use(
session({
secret: "something",
resave: true,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
//Connect with FLash
app.use(flash());
//Global variable used in flash
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});
// Serving static files
app.use(express.static(__dirname + "/public"));
app.use(express.static(__dirname + "/static/"));
app.use("/static", express.static(__dirname + "/static/"));
app.set("view engine", "ejs");
app.use(express.static('public'))
app.use('/css',express.static(__dirname +'public/css'))
app.use('/css',express.static(__dirname +'public/js'))
app.use('/css',express.static(__dirname +'public/images'))
app.use(methodOverride("_method"));
// deleteAllSubjects();
// deleteAllResources();
// deleteAllUsers();
//makeAdmins();
//makeSuperAdmins();
app.use("/", indexRoutes);
app.use("/subject", subjectRoutes);
app.use("/resource", resourceRoutes);
app.use("/users", userRoutes);
app.get("/404", (req, res) => {
res.send("<h1>404 not found</h1>");
});
app.listen(3000, process.env.IP, () => {
console.log("Server started at Port:3000");
});