-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (58 loc) · 1.88 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
const express = require("express");
const session = require("express-session");
const mongoose = require("mongoose");
const redis = require("redis");
const redisStore = require("connect-redis")(session);
const redisClient = redis.createClient();
const homeRouter = require("./routes/home");
const registerRouter = require("./routes/register");
const loginRouter = require("./routes/login");
const logoutRouter = require("./routes/logout");
const title = "index";
const app = express();
mongoose.connect("mongodb://localhost:27017/maschinenpark");
redisClient.on("error", (err) => {
console.log("\nCould not establish a connection with redis.\n".toUpperCase());
throw err;
});
app.set("views", "./views");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use("/css", express.static(__dirname + "public/css"));
app.use("/js", express.static(__dirname + "public/js"));
app.use("/img", express.static(__dirname + "public/img"));
app.use(express.urlencoded({ extended: false }));
app.use(
session({
secret: "Fkdj^45ci@Jad", // This is not a password
store: new redisStore({
host: "localhost",
port: 6379,
client: redisClient,
ttl: 260,
}),
rolling: true,
secure: false, // if true only transmit cookie over https
httpOnly: false, // if true prevent client side JS
resave: true,
saveUninitialized: false,
cookie: {
maxAge: 900000 // 15 minutes
},
})
);
app.get("/", (req, res) => {
if (req.session.authenticated) res.redirect("/home");
else res.render("index/index", { title: title });
});
app.use("/home", homeRouter);
app.use("/register", registerRouter);
app.use("/login", loginRouter);
app.use("/logout", logoutRouter);
try {
const server = app.listen(80)
console.info(`Listening on: http://localhost`);
} catch (e) {
console.log("There was an error starting the app");
console.log(e.message);
}