Api.
js
const express = require('express');
const router = [Link]();
const students = require('../data');
// Create a new student
[Link]('/students', (req, res) => {
const newStudent = [Link];
[Link] = [Link] + 1;
[Link](newStudent);
[Link](newStudent);
});
// Get all students
[Link]('/students', (req, res) => {
[Link](students);
});
// Get a single student
[Link]('/students/:id', (req, res) => {
const id = parseInt([Link]);
const student = [Link](s => [Link] === id);
if (!student) {
[Link](404).send({ message: 'Student not found' });
} else {
[Link](student);
});
// Update a student
[Link]('/students/:id', (req, res) => {
const id = parseInt([Link]);
const student = [Link](s => [Link] === id);
if (!student) {
[Link](404).send({ message: 'Student not found' });
} else {
[Link](student, [Link]);
[Link](student);
});
// Delete a student
[Link]('/students/:id', (req, res) => {
const id = parseInt([Link]);
const index = [Link](s => [Link] === id);
if (index === -1) {
[Link](404).send({ message: 'Student not found' });
} else {
[Link](index, 1);
[Link]({ message: 'Student deleted successfully' });
});
[Link] = router;
[Link]
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const apiRoutes = require('./routes');
[Link]([Link]());
[Link]('/routes', apiRoutes);
[Link](3000, () => {
[Link]('Server started on port 3000');
});
[Link]
const students = [
{ id: 1, name: 'John Doe', email: 'john@[Link]', rollNumber: 12345 },
{ id: 2, name: 'Jane Doe', email: 'jane@[Link]', rollNumber: 67890 },
];
[Link] = students;