NodeJS Slip 13B solution

//Slip_13B.js
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const port = 3000;

app.use(bodyParser.json());

let students = [
{ rno: 1, name: 'John Doe', marks: 85 },
{ rno: 2, name: 'Jane Doe', marks: 92 },
];

app.get('/', (req, res) => {
res.send('Welcome to the Student Information System');
});

app.get('/students', (req, res) => {
res.json(students);
});

app.put('/students/:rno', (req, res) => {
const { rno } = req.params;
const { marks } = req.body;

const student = students.find(s => s.rno == rno);

if (student) {
student.marks = marks;
res.json({ message: 'Marks updated successfully', updatedStudent: student });
} else {
res.status(404).json({ error: 'Student not found' });
}
});

app.listen(port, () => {
console.log('Server is running at http://localhost:${port}');
});

No comments:

Post a Comment