-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
66 lines (55 loc) · 2.42 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
const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const methodOverride = require("method-override");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 3000;
const connStr = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@ga.dijs6uo.mongodb.net/?retryWrites=true&w=majority`;
const multer = require("multer");
const upload = multer();
const auth_Middleware = require("./middlewares/auth_middleware");
const userController = require("./controllers/users/users_controller");
const pageController = require("./controllers/pages/page_controller");
const profileController = require("./controllers/profiles/profile_controller");
const cors = require("cors");
// Set view engine
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
app.use(cors());
app.use(methodOverride("X-HTTP-Method-Override"));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: false, httpOnly: false, maxAge: 7200000 },
})
);
app.get("/", pageController.showHome);
// Users Routes
app.get("/users/register", userController.showRegistrationForm);
app.post("/users/register", userController.register);
app.get("/users/login", userController.showLoginForm);
app.post("/users/login", userController.login);
app.post("/users/logout", userController.logout);
// profile
app.get("/profile", auth_Middleware.isAuthenticated, profileController.showInternalProfile);
app.get("/profile/edit", auth_Middleware.isAuthenticated, profileController.showEditInternalProfile);
app.post("/profile/edit", auth_Middleware.isAuthenticated, profileController.editInternalProfile);
app.post("/profile/delete", auth_Middleware.isAuthenticated, profileController.deleteInternalProfile);
app.get("/profile/uploadphoto", auth_Middleware.isAuthenticated, profileController.showUploadPhoto);
app.post("/profile/uploadphoto", auth_Middleware.isAuthenticated, upload.single("uploaded_file"), profileController.uploadPhoto);
// External
app.get("/:rfid", profileController.showExternalProfile);
app.listen(port, async () => {
try {
await mongoose.connect(connStr, { dbName: "tapcard" });
} catch (err) {
console.log(`Failed to connect to DB`);
process.exit(1);
}
console.log(`Example app listening on port ${port}`);
});