Assesment SetA-2

 


const http = require('http');
const studentDetails = {
    Id: '01',
    Name: 'Zaahid',
    Semester: 5,
    Batch: 2023
};

const server = http.createServer((req, res) => {
  if (req.method === 'GET') {
    res.end(studentDetails);
    console.log("GET")
  }
  else if (req.method === 'POST') {
    let body = '';
    req.on('data', (chunk) => {
      body += chunk;
    });
    req.on('end', () => {
      res.end(`POST request received with data: ${body}`);
    });
  }
  else if (req.method === 'PUT') {
    let body = '';
    req.on('data', (chunk) => {
      body += chunk;
    });
    req.on('end', () => {
      res.end(`PUT request received with data: ${body}`);
    });
  }
  else if (req.method === 'DELETE') {
    res.end('DELETE request received');
  }
  else {
    res.end('Not Found');
  }
});

const port = 3000;
server.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

Comments