// app.
js
document.addEventListener('DOMContentLoaded', () => {
const studentList = document.getElementById('student-list');
const addStudentForm = document.getElementById('add-student-form');
// Fetch all students from the backend
function fetchStudents() {
fetch('http://localhost:3000/api/students')
.then(response => response.json())
.then(data => {
studentList.innerHTML = '';
data.forEach(student => {
const li = document.createElement('li');
li.textContent = `${student.name} (${student.email})`;
studentList.appendChild(li);
});
})
.catch(err => console.error('Error fetching students:', err));
}
// Add a new student
addStudentForm.addEventListener('submit', (e) => {
e.preventDefault();
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
fetch('http://localhost:3000/api/students', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, email })
})
.then(response => response.json())
.then(() => {
fetchStudents(); // Refresh the list of students
})
.catch(err => console.error('Error adding student:', err));
});
fetchStudents(); // Initial fetch of students
});