Because we're using a React frontend, we'll revisit the idea of using Express as a backend with React proxying our API requests, as follows:
- The first thing we need to do is to tell our system to use a proxy by adding this line to our package.json: "proxy": "http://localhost:5000".
- After adding that, restart React (you'll notice that our frontend homepage has changed; we'll get to that in a moment) and then, in the api directory, execute npm install in the api directory and then npm start.
- We should test our backend to be sure our API is responding. Add this as a test to the App.js file after the imports:
fetch('/api')
.then(res => res.text())
.then(text => console.log(text))
This very basic fetch call should call the routes/index.js component's get method in our API:
router.get('/', (req, res) => {
res.sendStatus(200)
})
At this point, our console...