-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (100 loc) · 3.95 KB
/
Copy pathserver.js
File metadata and controls
122 lines (100 loc) · 3.95 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const { Pool } = require('pg');
const path = require('path'); // Import the path module
// Create a PostgreSQL connection pool
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'voter',
password: '1234',
port: 5432 // Default PostgreSQL port
});
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to parse JSON bodies
app.use(bodyParser.json());
app.post('/authenticate', async (req, res) => {
try {
const { image } = req.body;
// Fetch image data associated with the voterId from the database
const queryResult = await pool.query('SELECT image FROM vote WHERE image = $1', [image]);
if (queryResult.rows.length > 0) {
// Authentication successful
res.status(200).json({ success: true, message: 'Authentication successful' });
} else {
// Authentication failed
res.status(401).json({ success: false, message: 'Authentication failed' });
}
} catch (error) {
console.error('Error authenticating user:', error);
res.status(500).json({ success: false, message: 'Internal server error' });
}
});
app.post('/login', async (req, res) => {
try {
const voterID = req.body.voter-id; // Assuming the voter ID is submitted via a form field named 'voterID'
// Query the database to verify the voter ID
const { rows } = await pool.query('SELECT * FROM vote WHERE ID = $1', [voter-id]);
if (rows.length === 1) {
// Voter ID exists in the database, login successful
res.send('Login successful');
} else {
// Voter ID does not exist in the database, login failed
res.send('Invalid voter ID');
}
} catch (error) {
console.error('Error executing query', error);
res.status(500).send('Internal Server Error');
}
});
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Route for serving the login page
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'login.html'));
});
app.get('/ec', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'ec.html'));
});
app.get('/addcandidate', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'addcandidate.html'));
});
app.get('/ec2', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'ec2.html'));
});
app.get('/castvote', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'castvote.html'));
});
app.get('/hand', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'hand.html'));
});
app.get('/normal', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'normal.html'));
});
app.get('/results.html', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'results.html'));
});
app.get('/auth', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'html', 'auth.html'));
});
// Route for serving the normal authentication JavaScript file
app.get('/js/normal.js', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'js', 'normal.js'));
});
// Route for serving the blind authentication JavaScript file
app.get('/js/blind.js', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'js', 'blind.js'));
});
// Route for serving the hand disable authentication JavaScript file
app.get('/js/hand.js', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'frontend', 'js', 'hand.js'));
});
// Route for serving the root URL
app.get('/', (req, res) => {
res.send('Welcome to the voting application!'); // Or serve an HTML file or redirect to another route
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});