Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
Why is my react app not fetching my api data files?

I am still learning MERN stack and I was able to deploy applications twice. I am trying to deploy this MERN stack application but I have not been able to do that for hours.

I placed my reactjs build files in my api folder in a folder called build. This is my folder structure: enter image description here

In my app.js, I served my static files:

//routes
app.use("/api/v1/auth", authRoute);
app.use("/api/v1/users",userRoute);
app.use("/api/v1/posts",postRoute);

app.use(express.static(path.join(__dirname, '/build')))

 app.get('/', (req, res)=>{
res.sendFile(path.join(__dirname + '/build', 'index.html'));
})
app.use(checkDB);

module.exports = app;

In my reactjs, I called my one of my api endpoints like this:

const URL = 'http://localhost:5000/api/v1'

export default function Home() {
   useEffect( () => {
    dispatch({ type: "ISLOADING_START" });
    const fetchPosts = async ()=>{
    
   const res = await axios.get(URL+"/posts"+search)
    setPosts(res.data)
    dispatch({ type: "ISLOADING_END" });
    }
    fetchPosts()
     
}, [search])
  }

When I start my backend on port 5000, I tried to access my application but I can only get the header section of the html. enter image description here

The body remains empty and the page loads forever. It seems not able to fetch the api data. I sincerely dont know. I have watched videos on youtube and I can't seem to find solution.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You don't need a dedicated route for index.html. That is messing up with your static folder.

Just use serve static and it will handle serving index.html with everything listed in the folder.

This should be enough for you

app.use("/api/v1/auth", authRoute);
app.use("/api/v1/users", userRoute);
app.use("/api/v1/posts", postRoute);

app.use(express.static(path.join(__dirname, '/build')));

app.use(checkDB);

module.exports = app;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!