I’m working on a full-stack application using Express.js for the backend and React for the frontend. I need to consume data from a RapidAPI service but I’m having trouble displaying it properly in my React components. Right now the API response just shows as raw JSON data.
I have my Express server set up to fetch data from the API, but I’m not sure how to properly send this data to my React frontend and render it in a user-friendly way. What’s the best approach to handle this data flow?
Backend server code:
const express = require('express')
const cors = require('cors')
const axios = require('axios')
require('dotenv').config()
const app = express()
const PORT = 5000
app.use(cors())
app.get('/stats', async (req, res) => {
const config = {
method: 'GET',
url: 'https://api-football-v1.p.rapidapi.com/v3/teams/statistics',
params: {
league: '140',
season: '2023',
team: '42'
},
headers: {
'X-RapidAPI-Key': process.env.API_KEY,
'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com'
}
}
try {
const result = await axios.request(config)
console.log(result.data)
res.json(result.data)
} catch (err) {
console.error(err)
}
})
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})
React component:
import React from 'react'
function App() {
return (
<div>
{/* Need help displaying API data here */}
</div>
)
}
export default App