-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
app.js
61 lines (50 loc) · 1.32 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
//declaration
var express = require("express");
var expressSession = require("express-session");
var bodyParser = require("body-parser");
var app = express();
var port = 3000;
//common controllers
var signup = require("./controllers/signup");
var login = require("./controllers/login");
var logout = require("./controllers/logout");
//admin controllers
var admin = require("./controllers/admin");
//customer controllers
var customer = require("./controllers/customer");
//configure
app.set("view engine", "ejs");
//middlewares
app.use(bodyParser.urlencoded({ extended: false }));
app.use(
expressSession({
secret: "my top secret pass",
resave: false,
saveUninitialized: true,
})
);
app.use("/css", express.static(__dirname + "/css"));
app.use("/images", express.static(__dirname + "/images"));
app.use("*", function (req, res, next) {
if (req.originalUrl == "/login" || req.originalUrl == "/signup") {
next();
} else {
if (!req.session.admin && !req.session.customer) {
res.redirect("/login");
return;
}
next();
}
});
//routes
app.use("/login", login);
app.use("/signup", signup);
app.use("/logout", logout);
//admin routes
app.use("/admin", admin);
//customer routes
app.use("/customer", customer);
//server start
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});