forked from sontrinh16/student-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (44 loc) · 1.41 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
const express = require('express');
const bodyParser = require('body-parser');
const studentRouter = require('./routers/student');
const appError = require('./utils/appError');
const globalErrorHandler = require('./controllers/errorHandler');
const cookieParser = require('cookie-parser');
const path = require('path');
const authController = require('./controllers/authenticationController');
const teacherRouter = require('./routers/teacher');
const util = require('util');
const app = express();
app.use(bodyParser.json());
app.use(express.urlencoded({extended: true}));
app.use(cookieParser());
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/',async (req, res) => {
if (req.cookies.jwt)
{
res.status(200).redirect('/student/homepage');
}
else{
res.status(200).redirect('/login');
}
})
app.get('/login',async (req, res) => {
if (req.cookies.jwt)
{
return res.status(200).redirect('/student/homepage');
}
res.status(200).render('login',{
title: 'Login'
})
})
app.post('/login', authController.login);
app.use('/student', studentRouter);
app.use('/teachers', teacherRouter)
app.get('/logout', authController.logout)
app.all('*', (req, res, next) => {
next(new appError(404, 'invalid URL'));
});
app.use(globalErrorHandler);
module.exports = app;