forked from Vaishak-P/CHire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
133 lines (110 loc) · 4.2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const mysql = require('mysql');
const express = require('express');
const bodyparser = require('body-parser');
const path = require('path');
const { error } = require('console');
var app = express();
// Set view engine and configure body-parser
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'CHireMain'));
app.use(bodyparser.urlencoded({ extended: true }));
// Set up static files middleware
app.use(express.static(path.join(__dirname, 'CHireMain')));
// MySQL Connection Configuration
const mysqlConnection = mysql.createConnection({
connectionLimit: 100,
host: "127.0.0.1",
user: "newuser",
password: "Aswathy@2001",
database: "mydatabse",
port: "3306"
});
mysqlConnection.connect((err) => {
if (!err)
console.log('DB connection succeeded.');
else
console.log('DB connection failed \n Error: ' + JSON.stringify(err, undefined, 2));
});
app.get('/',(req,res)=>{
res.render('index')
})
app.get('/LOGIN',(req,res)=>{
res.render('LOGIN/login')
})
app.get('/LOGOUT',(req,res)=>{
res.render('index')
})
//For storing student details
let user = {};
// Login authentication route
app.post('/LOGIN', (req, res) => {
const email = req.body.email;
const password = req.body.psw;
mysqlConnection.query('SELECT * FROM users WHERE email = ?', email, (err, results) => {
if (err) {
console.error('Error executing query: ', err);
res.status(500).send('Internal Server Error');
return;
}
if (results.length === 0) {
// User not found
res.status(401).render('LOGIN/login', { error: 'Invalid credentials! Please try again.' });
return;
}
const login = results[0];
if (login.password !== password) {
// Incorrect password
res.status(401).render('LOGIN/login', { error: 'Invalid credentials! Please try again.' });
return;
}
// Authentication successful
// Redirect based on user's role
switch (login.role) {
case 'student':
// Redirect to student dashboard
mysqlConnection.query('SELECT * FROM student WHERE email = ?', email,(err, results)=>{
if (err) {
console.error('Error executing query: ', err);
res.status(500).send('Internal Server Error');
return;
}
user = results[0]
res.render('std-dashboard/std-dashboard', {userName:user.name,cgpa:user.cgpa,sem:user.sem,test:user.mocktest_score,fluency:user.fluency_score,internships:user.internships,phn:user.phone,mail:user.email,address:user.address,bld:user.blood_group,total:user.total });
});
break;
case 'placementOfficer':
// Redirect to placement coordinator dashboard
mysqlConnection.query('SELECT * FROM placement_officer WHERE email = ?', email,(err, results)=>{
if (err) {
console.error('Error executing query: ', err);
res.status(500).send('Internal Server Error');
return;
}
user = results[0]
console.log(user)
res.render('po/dashboard');
});
break;
case 'company':
// Redirect to company HR dashboard
res.redirect('/company-hr/dashboard');
break;
default:
// Handle unknown designation
res.status(500).send('Unknown user designation');
}
});
});
app.get('/MockTest/:heading', (req, res) => {
const encodedHeading = req.params.heading;
const decodedHeading = decodeURIComponent(encodedHeading);
res.render("std-test-details/std-test-details",{heading:decodedHeading})
});
const studentRouter = require('./routes/student')
const poRouter = require('./routes/po')
app.use(studentRouter)
app.use(poRouter)
// Start the server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});