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 {
background-color: #3949AB;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #303F9F;
}
ul {
list-style-type: none;
}
</style>
</head>
<body>
<div class="container">
<h2>Register Student</h2>
<form id="registerForm">
<label for="studentname">Student Name:</label>
<input type="text" id="studentname" name="studentname" required>
<button type="submit">Register</button>
</form>
<h2>Enroll in Class</h2>
<form id="enrollForm">
<label for="studentid">Student ID:</label>
<input type="text" id="studentid" name="studentid" required>
<label for="classid">Class ID:</label>
<input type="text" id="classid" name="classid" required>
<button type="submit">Enroll</button>
</form>
<h2>Available Classes</h2>
<button id="fetchClasses">Show Classes</button>
<ul id="classesList"></ul>
</div>
<script>
document.getElementById('registerForm').addEventListener('submit', function (e) {
e.preventDefault();
let studentname = e.target.studentname.value;
fetch('/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ studentname })
}).then(res => res.text()).then(alert);
});
document.getElementById('enrollForm').addEventListener('submit', function (e) {
e.preventDefault();
let studentid = e.target.studentid.value;
let classid = e.target.classid.value;
fetch('/enroll', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ studentid, classid })
}).then(res => res.text()).then(alert);
});
document.getElementById('fetchClasses').addEventListener('click', function () {
fetch('/classes').then(res => res.json()).then(data => {
let ul = document.getElementById('classesList');
ul.innerHTML = '';
data.forEach(item => {
let li = document.createElement('li');
li.textContent = `Class ID: ${item.classid}, Name: ${item.classname}, Location: ${item.roomlocation}, Date & Time: ${item.classdatetime}`;
ul.appendChild(li);
});
});
});
</script>
</body>
</html>
javascriptCopy code
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;
}