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

376
Visualizações
How to display static files from backend into React?

I have a React application with an Express-Node.js backend. I have a proxy that links both. Now, I have some static files in a directory within the backend. When I make the request and place the response in the "src" attribute of an image tag, I can display the image, but same result does not happen when I try the link in the browser.

This works in the browser: http://localhost:5000/public/image.png /EXAMPLE ROUTE/

This doesn't: https://localhost/image.png /EXAMPLE ROUTE/

My backend code:

const express = require('express');
const app = express();
const PORT = process.env.PORT;

app.use('/images', express.static(__dirname + '/public'));

app.listen(PORT, (err) => {
  if(err) {
    throw err;
  } else {
    console.log(`Running on http://localhost:${PORT}/`);
  }
});

My react code:

import React from 'react';
import './App.css?v=1';

function App() {
    return (
        <div className="App">
            <img src="/images/image.png" alt="Logo" /> /*THIS TOTALLY WORKS*/
            <img src="https://localhost/images/image.png" alt="Logo" /> /*THIS TOTALLY WORKS*/
        </div>
    );
}

export default App;

My problem comes when I go to the browser and type the URL: https://localhost/images/image.png -> This will not display the image the way http://localhost:5000/public/image.png will and all I get is a blank page.

In React's package.json file, I proxy to the backend.

If clarification is needed, let me know. Thanks, in advance.

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

0

do like me

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


 this.app.use("/public", express.static(path.join(__dirname, "public")));


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