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

226
Visualizações
image is not getting loaded from the express static Folder

Images is getting loaded from the local Folder But not from the uploads Folder which is declared static.

I have been trying to upload file from the browser, The image is uploaded sucessfully and saved successfully But while rendering the images from the uploads folder is not getting rendered

This is how the first image which is in src/public is rendered whereass the uploads/images are not rendered

Sample Data from MongoDb Database

_id:1
...
image:"/images/mouse.jpg"
...

_id:2
...
image:"/uploads/image-1647229113730.jpeg"
...

Product Component

const Product = ({ product }) => {
  return (
    <Card className='my-3 p-3 rounded'>
      <Link to={`/product/${product._id}`}>
        <Card.Img src={product.image} variant='top' />
      </Link>
      <Card.Body>
        ...
      </Card.Body>
    </Card>
  )
}

export default Product

The File herarchy is as follows

Repo
 | backend
   | server.js
 | frontend
   | public
     | images
        |mouse.jpg
 | uploads
   | image-1647229113730.jpeg

In server.js in the backend i have made the uploades folder static

const __dirname = path.resolve();
app.use("/uploads", express.static(path.join(__dirname, "/uploads")));

My problem is simple ,

/images/mouse.jpg is accessible | /uploads/image-1647229113730.jpeg is not accessible

though both are there in the projects folder why ?

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

0

In your server.js you are declaring __dirname which overrides the implicitly declared __dirname that node provides for you.

So instead of using the value of path.resolve() which is the current working directory as described here you shouldn't declare __dirname yourself and use the __dirname that node provides. That is the path where your source file is located.

Then you can use path.join(__dirname,'../uploads') to get the path of your uploads folder. (The two dots are important)

EDIT:

I'm sorry. I've completely forgotten that __dirname isn't available in ES modules...

But here is another way that could work:

import { URL } from 'url';
const __dirname = new URL('.', import.meta.url).pathname;

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

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