Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

386
Visualizações
Error: Network Error with React, Express, Axios

I'm trying to run a website on an AWS EC2 instance using React, Express and Axios.

The problem: I'm unable to make axios calls to the Express back-end running on the same instance. The code works fine on my local machine, but on the EC2 instance I get Error: Network Error. The server runs on port 5000 and the client runs on port 3000. I'm able to access the website from the outside using the URL, and am able to make GET requests to the back-end directly using Postman.

Home.js (frontend)

import { useState, useEffect } from 'react';
import axios from 'axios';

function Home() {
    const [text, setText] = useState('');

    useEffect(() => {
        axios.get(`http://172.31.20.10:5000/`)
            .then(
                result => {
                    setText(result.data);
                },
                err => {
                    alert(err);
                    setText('An error has occurred');
                }
            );
    });

    return (
        <h1>{ text }</h1>
    );
}

export default Home;

App.js (frontend)

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Home from './Home';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <Home />
  </React.StrictMode>,
  document.getElementById('root')
);

reportWebVitals();

App.js (backend)

const express = require('express');
const cors = require('cors');
const app = express()
const port = 5000;

app.use(express.json());
app.use(cors());

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen(port, () => {
    console.log(`App listening on http://localhost:${port}`);
});

172.31.20.10 is the private IP of the EC2 instance. I've tried using localhost instead of the IP, https:// instead of http://, not use http(s) at all, leaving the http and IP/localhost out entirely, etc etc. None have worked so far. Any help would be much appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To answer the comments: The code did work on my own computer, and the whole network error was a CORS error ("CORS request did not go through" or some such).

I'm still not exactly sure what went wrong, but I "solved" it by hosting the client and server on two different instances. Not sure how or why it works, but it did...

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda