Question on req.body with react

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

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 etc. like you mentioned.'/signup', (req, res) => {
  const body = req.body;

I am not sure why you want to access it outside of the 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 = {};'/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());"/posts", (req, res) => {
    // console.log("Post data : ",
    // console.log("JSON Response : ",res.json());
  });"/api/agreement", (req, res) => {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, application/json")
    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 (
        <p>Post data {this.props.postId}</p>

export default Posts;

Any help or suggestion is much appreciated. Thank you.