-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (40 loc) · 1.35 KB
/
server.js
File metadata and controls
47 lines (40 loc) · 1.35 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
const express = require('express');
const { Client } = require('pg');
const path = require('path'); // Import the 'path' module
// Rest of your code...
const app = express();
const port = 3000;
app.use(express.json()); // Parse JSON bodies
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded bodies
// PostgreSQL connection
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'postgres',
password: 'jhilu923',
port: 5432,
});
client.connect();
app.use(express.static(path.join(__dirname, 'public')));
// API endpoint to store quiz marks
app.get('/quizmarks', (req, res) => {
res.sendFile(__dirname + '/quiz2.html');
});
app.post('/api/quizmarks', async (req, res) => {
const { userId, quizScore } = req.body;
try {
// Insert quiz marks into the database
const query = {
text: 'INSERT INTO quiz_results (user_id, marks) VALUES ($1, $2)',
values: [userId, quizScore],
};
await client.query(query);
res.json({ success: true, message: 'Quiz marks stored successfully' });
} catch (error) {
console.error('Error storing quiz marks:', error);
res.status(500).json({ success: false, message: 'Failed to store quiz marks' });
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});