-1

My Front-end Code:-

const [stocks, setStocks] = useState([

    {
        "name": "TSLA"
    },
    {
        "name": "AAPL"
    },
    {
        "name": "AMZN"
    }
])


useEffect(() => {
    fetch('http://localhost:2701/' , {
        method: "POST",
        headers: {
            'Content-type': 'application/json'
        },
        body: JSON.stringify(stocks)
        })
        .then((result) => {
            console.log(result)
        })
        .then((info) => { console.log(info); })
})

My Back-end Code:-

router.post('/', async (req, res) => {
    var data = req.body.data;
    console.log("Data:-", data)
})

I want to send values from reactjs to nodejs server, if possible can anyone suggest using axios

Maharsh Gheewala
  • 2,091
  • 2
  • 10
  • 6

1 Answers1

0

Pls try the code below.

import axios from 'Axios';
...
    axios.post('http://localhost:2701/').then(response => {
      console.log(response);
    });
Keerthi Vijay
  • 356
  • 2
  • 9