const express = require('express');
const bodyParser = require('body-parser');
const sqlite3 = require('sqlite3').verbose();
const app = express();
const PORT = 3000;
console.log("Starting server...");
// Middleware to parse POST data
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // to parse JSON data
// Middleware to serve static files from the 'public' directory
app.use(express.static('public'));
// Database setup
const db = new sqlite3.Database('./ces.db', (err) => {
if (err) {
console.error("Error connecting to the ces SQLite database:", err.message);
return;
}
console.log('Connected to the ces SQLite database.');
});
// Route to serve index.html
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// Route to register a new student
app.post('/register', (req, res) => {
const { studentname } = req.body;
db.run(`INSERT INTO students(studentname) VALUES(?)`, [studentname], (err) => {
if (err) {
console.error("Error registering student:", err.message);
res.status(500).send('An error occurred while registering student.');
return;
}
res.send('Student registered successfully!');
});
});
// Route to list all classes
app.get('/classes', (req, res) => {
db.all(`SELECT * FROM classes`, [], (err, rows) => {
if (err) {
console.error("Error fetching classes:", err.message);
res.status(500).send('An error occurred while fetching classes.');
return;
}
res.json(rows);
});
});
// Route to enroll a student in a class
app.post('/enroll', (req, res) => {
const { studentid, classid } = req.body;
db.run(`INSERT INTO enrollments(studentid, classid) VALUES(?, ?)`, [studentid, classid], (err) => {
if (err) {
console.error("Error enrolling student:", err.message);
res.status(500).send('An error occurred while enrolling the student.');
return;
}
res.send('Student enrolled successfully!');
});
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CES Party</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #FFE082;
background-image: linear-gradient(45deg, #FFD54F, #FF6E40);
animation: gradientBG 5s infinite linear alternate;
}
@keyframes gradientBG {
0% {
background-image: linear-gradient(45deg, #FFD54F, #FF6E40);
}
100% {
background-image: linear-gradient(45deg, #FF6E40, #FFD54F);
}
}
.container {
max-width: 600px;
margin: 2rem auto;
background-color: rgba(255, 255, 255, 0.9);
padding: 2rem;
border-radius: 15px;
}
h2 {
text-align: center;
color: #3949AB;
}
button {
