I am developing an api in express and nodeJS. In addition to that I have a folder with images that is related to a document saved in mongoDB, I have no problem querying the data. But I can't get the image via the URL:
http://localhost:3000/imagenes/Q5l_15xBI0hhm-iyaT6hMCvm.jpg
The result I get in the browser is:
Cannot GET /imagenes/Q5l_15xBI0hhm-iyaT6hMCvm.jpg
.
I already created the path for that directory like this:
app.get('/:img', function(req, res){
res.sendFile( `imagenes/${img}` );
});
and use the static Middleware from express:
app.use(express.static('imagenes'));
for the directory, but I still get the 404 and I can't display the image.
Clarification: the folder that contains the images is at the same level as my js file that executes this
The problem was in a nonsense, the URL says:
and the path says:
the route omitted the /images route
Actually, your code is correct, all you need is where the file is located.
this is how it is done correctly