Getting Data in Request Body in Instagram Clone

 Step 01: Create the folder named routes and create file auth.js in that 

 Step 02: We need to require express and use it in router 

const express = require('express')
const router = express.Router()

 Step 03:  Create a route and also export that route

router.get('/',(req,res)=>{
res.send("hello user")
})


Step 04: Now We can Register this route in App.js:

app.use(require('./routes/auth'))

Step 05: Make the Signup Route basically with console.log and test it at the Postman

router.post('/signup',(req,res)=>{
console.log(req.body.name)
})

In Postman-

Headers : content-Type:Application/json

Body: {

name:"mukesh"

}

but it will not work in response because in postamn 

our express server does not pass the request to the json , we need to tell it to the server 

app.use(express.json())

this is a type of middleware and i will write it in the app.use - because we want to take  all the incoming request that  to the json 

router.post('/signup',(req,res)=>{
console.log(req.body.name)
})

and change it to the req.body


router.post('/signup',(req,res)=>{
console.log(req.body)
})

and the postman will be hang after hitting the request 

I will use express.json before the route not after the route and we have to take care about it-Order obviously Matter.


app.use(express.json())
app.use(require('./routes/auth'))








Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5