I’m working on a web application that uses Express for the backend and React for the frontend. My goal is to retrieve data from a RapidAPI endpoint and display it correctly in my React components, rather than just showing the raw JSON data.
Currently, my Express server is successfully obtaining the API data, but I’m unsure of how to utilize this data in my React frontend and present it properly within HTML elements.
Backend code (app.js
):
const express = require('express')
const cors = require('cors')
const axios = require('axios')
require('dotenv').config()
const server = express()
const PORT = 3001
server.use(cors())
server.get('/players', async (req, res) => {
const config = {
method: 'GET',
url: 'https://api-football-v1.p.rapidapi.com/v3/players/topscorers',
params: {
league: '140',
season: '2023'
},
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);
}
})
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})
Frontend code (Main.js
):
import React from 'react'
function Main() {
return (
<div>
{/* Need help displaying API data here */}
</div>
)
}
export default Main
What is the best way for me to fetch data from my Express endpoint and display it properly in React components?