Question on req.body with react

Hi…I am new to react js…how to extract data from req.body?is it accessible outside app.post?

Hello, you originally posted your question in an older thread, but I have moved it to a new one as it was somewhat unrelated to the old thread and you should get faster help this way.


I don’t think req.body is concerned with React, as I’m guessing you are referring to your express API or server. Once you include the body parser middleware in your express app (npm install body-parser and the require and use it), you can access it with req.body inside app.post etc. like you mentioned.

app.post('/signup', (req, res) => {
  const body = req.body;
});

I am not sure why you want to access it outside of the app.post function, but I think you could by assigning it to a variable in a higher scope. You would then have to worry about it being empty with asynchronous callbacks and such.

let body = {};

app.post('/signup', (req, res) => {
  body = req.body;
});

Can you provide more info on what you are trying to do?

Hi, I am trying to do something similar I have a next js app on which an android app would post some json data in web view. I am able to console this data on my server now I would like to pass this data to my client which would use the data to route to different routes based on the props.
Following is my server.js code:-

const express = require('express')
const next = require('next')

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

let agreement={};

app.prepare().then(() => {
  const server = express()

  server.use(express.json());

  server.post("/posts", (req, res) => {
    req.body.data
    // console.log("Post data : ",req.body.data)
    // console.log("JSON Response : ",res.json());
  });

  server.post("/api/agreement", (req, res) => {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, application/json")
    req.body.data
    res.send(req.body.data)
    res.json()
    agreement= req.body.data;
    console.log("Post data agreement: ",agreement)
    // console.log("JSON Response agreement : ",res.json());
  });


  server.get('*', (req, res) => {
    return handle(req, res)
  })

  server.listen(port, err => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  })
})

Now I would like to know how I can access this data on a particular web page preferably in getInitial props.
Following is my code for my page:-

import React, { Component } from 'react'

class Posts extends Component {
  static async getInitialProps ({ query: { id } }) {
    console.log("Response is ")
    return { postId: id }
  }
  render () {
    return (
      <div>
        <p>Post data {this.props.postId}</p>
      </div>
    )
  }
}


export default Posts;

Any help or suggestion is much appreciated. Thank you.