Sunday, January 14, 2024

Day 27 || Express js || Express Framework || Get method in Express || Post Method in Express

 Node.js Topics

Day 27:

  • Express Framework
    • Get
    • Post
Express.js

const express = require('express');
const callingData = require('./Data.js')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))
app.get('/',(req,res)=>{
res.send("Hello, Hema coding school");
})
app.get('/about',(req,res)=>{
    res.send("About , Hema coding school")
})
app.get('/item',(req,res)=>{
    res.send(`
    <h1>Hema Codning School</h1>
    <h2>${callingData[0].id}</h2>
    <h2>${callingData[0].name}</h2>
    <h2>${callingData[1].id}</h2>
    <h2>${callingData[1].name}</h2>
    <h2>${callingData[2].id}</h2>
    <h2>${callingData[2].name}</h2>
    `)
})

app.post('/api',(req,res)=>{
    newData={
        id:req.body.id,
        name: req.body.name
    }
    callingData.push(newData)
    res.json(callingData)
})
const PORT = 3000;
const HOSTNAME = "127.0.0.1";
const BACKLOG = 551;

app.listen(PORT, HOSTNAME, BACKLOG,()=>{
    console.log(`Server is running at http://${HOSTNAME}:${PORT}`)
})


Data.js

const items = [
    {
      id: 1,
      name: "Hema",
    },
    {
      id: 2,
      name: "Mahesh",
    }
   
  ];

  module.exports = items


Interview Questions:

1. How does routing work in Express.js?

2. What is middleware in Express and how is it used in the context of a GET request?

3. How do you handle a POST request in Express?






No comments:

Post a Comment

Top 10 | JavaScript Coding Interview Question | Beginner Level

               JavaScript Coding Interview  Q. No. 01/10:  console . log ( "1" + "4" + "1" ) // 141 console . ...