10 sdc

 PROGRAM 10. Develop an express web application that can interact with REST API to perform1 4 CRUD operations on student data. (Use Postman).

Server.js

const express = require('express');

const app = express();

app.use(express.json());

let students = [];

// CREATE

app.post('/students', (req, res) => {

    const student = {

        id: students.length + 1,

        name: req.body.name,

        course: req.body.course

    };

    students.push(student);

    res.send("Student Added Successfully");

});

// READ

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

    res.json(students);

});

// UPDATE

app.put('/students/:id', (req, res) => {

    const id = parseInt(req.params.id);

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

    if(student){

        student.name = req.body.name;

        student.course = req.body.course;

        res.send("Student Updated");

    }

    else{

        res.send("Student Not Found");

    }

});

// DELETE

app.delete('/students/:id', (req, res) => {

    const id = parseInt(req.params.id);

    students = students.filter(s => s.id !== id);

    res.send("Student Deleted");

});

app.listen(3000, () => {

    console.log("Server running on port 3000");

});


Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

BANKING SYSYTEM DATABASE WITH RECORDS

CLL by smd