Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (76 loc) · 2.32 KB
/
Copy pathserver.js
File metadata and controls
92 lines (76 loc) · 2.32 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
const express = require('express');
const app = express();
const PORT = 8080;
// Middleware to parse JSON bodies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files from the 'public' directory
app.use(express.static('public'));
// In-memory storage for survey responses
let surveyResponses = [];
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', message: 'Server is running' });
});
// Get all survey responses
app.get('/api/responses', (req, res) => {
console.log('📊 Fetching all responses...');
res.json({
total: surveyResponses.length,
responses: surveyResponses
});
});
// Submit a new survey response
app.post('/api/submit', (req, res) => {
const { name, rating, comment } = req.body;
if (!name || !rating) {
return res.status(400).json({ error: 'Name and rating are required' });
}
const response = {
id: Date.now(),
name,
rating: parseInt(rating),
comment: comment || '',
timestamp: new Date().toISOString()
};
surveyResponses.push(response);
console.log('✅ New survey response received:');
console.log(` Name: ${name}`);
console.log(` Rating: ${rating}/5`);
console.log(` Comment: ${comment || 'No comment'}`);
console.log(` Total responses: ${surveyResponses.length}`);
console.log('---');
res.json({
success: true,
message: 'Survey response recorded',
response
});
});
// Get survey statistics
app.get('/api/stats', (req, res) => {
if (surveyResponses.length === 0) {
return res.json({
total: 0,
averageRating: 0,
distribution: { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 }
});
}
const totalRating = surveyResponses.reduce((sum, r) => sum + r.rating, 0);
const averageRating = (totalRating / surveyResponses.length).toFixed(2);
const distribution = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };
surveyResponses.forEach(r => {
distribution[r.rating]++;
});
res.json({
total: surveyResponses.length,
averageRating: parseFloat(averageRating),
distribution
});
});
// Start the server
app.listen(PORT, () => {
console.log('🚀 Course Satisfaction Survey Application');
console.log(`📍 Server running on http://localhost:${PORT}`);
console.log('💾 Using in-memory storage (data will be lost on restart)');
console.log('---');
});