Assignment 3(or 4)

 


//01 Create a program which request the resource from the server on the
//   route /hello using get method as well as post method.
const express = require('express');
const app =express();

app.get('/hello',function(req,res){
    res.send("Hello Zaahid")
})
app.post('/hello',function(req,res){
    res.send("Hello Zaahid")
})
app.listen(3000);

//02 Create a program which request the resource from the server
//   using get, post, delete and put methods.
//<---Error code dont use--->
const express = require('express');
const app = express();

//
app.get('/get', (req, res) => {  
  const studentDetails = {
    Id: '01',
    Name: 'Zaahid',
    Semester: 5,
    Batch: 2023
};
    res.send(studentDetails);  
    console.log("Get");
});

//
app.post('/post', (req, res) => {
 // const data = req.body;
  res.send("POST Response");
  console.log("Post");
});

//
app.delete('/delete', (req, res) => {
  const studentDetails = req.params.id;
  res.send('DELETE Response');
  console.log("Delete");
});

//
app.put('/put', (req, res) => {
  const studentDetails = req.params.id;
  const data = req.body;
  res.send('Put');
});


app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

//03 Create a program which send the detail of a student as a JSON object to the client.
const express = require('express');
const app = express();

app.get('/student', (req, res) => {
    const studentDetails = {
        Id: '01',
        Name: 'Zaahid',
        Semester: 5,
        Batch: 2023
    };
    res.json(studentDetails);
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

//05 Create a Simple Middleware to record every time you a get a request on the server.
const express = require('express');
const app = express();
const fs = require('fs')


const outputLog = fs.createWriteStream('./outputLog.log');
const errorsLog = fs.createWriteStream('./errorsLog.log');
const consoler = new console.Console(outputLog, errorsLog);

const datalog = (req, res, next) => {
  const timestamp = new Date().toISOString();
  const method = req.method;
  const url = req.originalUrl;

  consoler.log(`[${timestamp}] ${method} ${url}`);
  next();
};

app.use(datalog);

app.get('/url', (req, res) => {
  res.send('Hello, world!');
});

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

Comments