I am new to the front end technologies. My requirement is to make a post call to an external server. I started with creating the react app and used axios to make the call, but I was getting access denied due to CORS. Some posts on stackoverflow mentioned that I would need a proxy server to overcome this. Thus, I added an express part to the project as well. Below is my react and express codes.
React:
import React, { Component } from "react";
class App extends Component {
state = { posts: null };
componentDidMount() {
fetch("/posts")
.then(res => {
console.log(res);
return res;
})
.then(posts => this.setState({ posts: posts }));
}
render() {
return (
<div className="App">
<h1>Users</h1>
{this.state.posts}
</div>
);
}
}
export default App;
Express:
var express = require("express");
var router = express.Router();
const axios = require("axios");
router.get("/", function(req, res, next) {
axios
.get("https://jsonplaceholder.typicode.com/posts")
.then(response => res.json(response.data))
.catch(err => next(err));
});
module.exports = router;
When I load the React app, I get the below error:
Error: Objects are not valid as a React child (found: [object Response]). If you meant to render a collection of children, use an array instead.