-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (63 loc) · 2.57 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
const express = require('express');
const connectDB = require('./db');
const app = express();
const cookieParser = require('cookie-parser');
const { adminAuth, userAuth, staffAuth, sessionAuth } = require('./middleware/auth.js');
const { port } = require('./config.json');
const { join } = require('node:path');
// Setting View Engines
app.set('view engine', 'ejs');
app.set('views', join(process.cwd(), '/web-server-views/views'));
// Connecting to Database
connectDB();
// Middlewares
app.use(express.json());
// https://www.tutorialspoint.com/expressjs/expressjs_form_data.htm (For Serverside authentication)
// https://expressjs.com/en/resources/middleware/body-parser.html#bodyparsertextoptions
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Routes
app.use('/api/auth', require('./Auth/route'));
app.use('/api/amd-r', require('./amd-r/route'));
// Home Page
app.get('/', (req, res) => res.render('auth/home'));
// Authentication Requests
app.get('/register', sessionAuth, (req, res) => {
if (req.cookies.redirect) {
res.clearCookie('redirect');
res.render('auth/register-failed');
} else {
res.render('auth/register');
}
});
app.get('/login', sessionAuth, (req, res) => {
if (req.cookies.redirect) {
res.clearCookie('redirect');
res.render('auth/login-failed');
} else {
res.render('auth/login');
}
});
app.get('/logout', (req, res) => {
res.cookie('jwt', '', { maxAge: '1' });
res.redirect('/');
});
app.get('/admin', adminAuth, (req, res) => res.render('auth/admin'));
app.get('/admin/users', adminAuth, (req, res) => res.render('auth/admin-users'));
app.get('/admin/amd-r', adminAuth, (req, res) => res.render('auth/admin-amd-r'));
app.get('/amd-r/*', adminAuth, (req, res) => res.render('amd-r/amd-r'));
app.get('/basic', userAuth, (req, res) => res.render('auth/user'));
app.get('/staff', staffAuth, (req, res) => res.render('auth/staff'));
// External js, media and css request
// https://www.codespeedy.com/how-to-serve-html-and-css-files-using-express-js/
app.get('/css/*', (req, res) => res.sendFile(join(__dirname, '/web-server-views', req.path)));
app.get('/js/*', (req, res) => res.sendFile(join(__dirname, '/web-server-views', req.path)));
app.get('/media/*', (req, res) => res.sendFile(join(__dirname, '/web-server-views', req.path)));
app.get('*', (req, res) => res.status(404).render('error/404'));
const server = app.listen(port, () =>
console.log(`Server Connected to port ${port}`),
);
process.on('unhandledRejection', (err) => {
console.log(`An error occurred: ${err.message}`);
server.close(() => process.exit(1));
});