Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

349
Vistas
Unable to call internal API with create-react-app

I'm having an issue calling fetch('/api') from my very basic create-react-app.

The issue I'm getting in console is GET http://localhost:3000/api 500 (Internal Server Error)

In my client/package/json I'm setting the proxy to "proxy": "http://localhost:3001", yet when I call /api it's trying to use port 3000 rather than 3001.

I have two servers running, the client at 3000 and the API at 3001 and when I go to localhost:3001/api I see the expected {message: "Hello from server!" } json in my browser

My client/src/App.js:

import React from "react";
import logo from "./logo.svg";
import "./App.css";

function App() {
  const [data, setData] = React.useState(null);

  React.useEffect(() => {
    fetch("/api")
      .then((res) => res.json())
      .then((data) => setData(data.message));
  }, []);

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>{!data ? "Loading..." : data}</p>
      </header>
    </div>
  );
}

export default App;

My server/index.js:

const path = require('path');
const express = require("express");

const PORT = process.env.PORT || 3001;

const app = express();

// Have Node serve the files for our built React app
app.use(express.static(path.resolve(__dirname, '../client/build')));

app.get("/api", (req, res) => {
  res.json({ message: "Hello from server!" });
});

app.listen(PORT, () => {
  console.log(`Server listening on ${PORT}`);
});

Would love to know what I'm doing wrong here.

Thanks!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can modify your fetch request API url to give the complete hostname since

 fetch('http://localhost:3000/api/') 

Or you can use third-party proxy middleware. see this post https://medium.com/bb-tutorials-and-thoughts/react-how-to-proxy-to-backend-server-5588a9e0347

over 4 years ago · Santiago Trujillo Denunciar

0

The "proxy" setting described in the documentation should be set in the server package.json, not the client's. This is because the way the proxy work is that the server accepts the incoming request (on port 3000) and then turns around and proxies it to port 3001 on your behalf.

The client doesn't know it's being proxied.

Relevant bit from the documentation, with emphasis added:

To tell the development server to proxy any unknown requests to your API server in development, add a proxy field to your package.json...

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda