skip to Main Content

I have a node.js server with express and the HTML page with some JavaScript, CSS, and all that. Some weeks ago I had a requirement to add images to the page, I receive the name of the image and I have to show it, also I know the directory where it is stored. The problem is that, when I was testing, I added an image to that folder and I show it when entering the page. Now I have added some more images, and I am trying to show them, but it does not work. The error is this:

Error: EACCES: permission denied, open ‘/fotos/Abstract-sony.jpg’

The image that I added before still works, but the new one does not. I gave them the same permissions, so it should not be that. The directories structure I am using is this:

/Images
/Server
  --app.js
  --package.json
  /node_modules
  /client
      --index.html
      /js
      /css
      /imgs

Yes, I have two folders with images, the one inside client gives no problem, the other one is the issue, and no, I can not simply move the images to /imgs. The server code is this:

const http = require("http");
const sio = require("socket.io");
const express = require("express");

const app = express();
const SioServer = http.createServer(app)

const io = sio(SioServer);
const sioPort = 3000;

app.use(express.static(__dirname + '/client'))
app.use(express.static("/images/"))

io.on("connection", (socket)=>{
  console.log("IO user connected")
    socket.on('client_data', (id) => {
      var userId = id;
      socket.join(userId)
    });
    socket.on("lastImage",(msg)=>{
        console.log(msg)
        io.to("Web").emit("lastImage", msg)
    })
  });

app.get("/", (req,res)=>{
    res.sendFile(__dirname + '/client/index.html')
})

//Lanzamos el servidor en el puerto 3000
SioServer.listen(sioPort,()=> {
    console.log(`Listening on ${sioPort}`)
})

On the web side, I have HTML with an and a JavaScript with something like this:

function init() {
    image = document.getElementById("image");
    image.setAttribute("src", "/test.jpg");
}
socket.on("lastImage", (msg)=>{
    image = document.getElementById("image");
    image.setAttribute("src", "/"+msg);
})

With this, test.jpg shows up on the page, but whenever a message with a new image arrives, it throws the error from before. If I try to show the new images from the start, it also throws the error.

Also, the permissions of the images (yes, they are wallpapers, it is what I had in hand):

drwxr-xr-x 1 root vboxsf       750 jun 23 11:29 .
drwxr-xr-x 1 root root         196 jun  7 17:05 ..
-rwx------ 1 root vboxsf    191120 jun 23 11:29 1920x1080_1644315267727.jpeg
-rwx------ 1 root vboxsf    136744 jun 23 11:29 1920x1080_1646185285507.jpeg
-rwx------ 1 root vboxsf     50212 jun 23 11:29 1920x1080_1646185504142.jpeg
-rwx------ 1 root vboxsf     67215 jun 23 11:29 1920x1080.jpg
-rwx------ 1 root vboxsf    662321 jun 23 11:29 1920x1080.png
-rwx------ 1 root vboxsf   1489068 jun 23 11:29 2560x1600.png
-rwx------ 1 root vboxsf   2514658 jun 23 11:29 5120x28800.png
-rwx------ 1 root vboxsf   4160783 jun 23 11:29 5120x2880.jpg
-rwx------ 1 root vboxsf   4370767 jun 23 11:29 5120x2880.png
-rwx------ 1 root vboxsf   1204050 jun 23 11:29 8000x4500.png
-rwx------ 1 root vboxsf    866785 jun 23 11:29 848791.jpg
-rwx------ 1 root vboxsf    166850 jun 23 11:29 Abstract-sony.jpg
-rwx------ 1 root vboxsf    345646 jun 23 11:29 Blue_and_Yellow 01.jpg
-rwx------ 1 root vboxsf   4247411 jun 23 11:29 Carl.png
-rwx------ 1 root vboxsf    626061 jun 23 11:29 future-28.jpg
-rwx------ 1 root vboxsf   2858273 jun 23 11:29 Helado-5120x2880.png
-rwx------ 1 root vboxsf     31124 jun 23 11:29 nordic-wallpaper.jpg
-rwx------ 1 root vboxsf    242094 jun 23 11:29 Onda-1920x1080.jpg
-rwx------ 1 root vboxsf    437369 mar  6 21:04 prueba.jpg
-rwx------ 1 root vboxsf     90433 jun 23 11:29 Smite-Loki-Wallpaper-09053.jpg
-rwx------ 1 root vboxsf    258213 jun 23 11:29 t5CrPk.jpg
-rwx------ 1 root vboxsf 470336768 jun 15 10:53 test.mp4
-rwx------ 1 root vboxsf    211231 jun 23 11:29 wp3330706.jpg

2

Answers


  1. Chosen as BEST ANSWER

    The solution was to create a new user and give it the ownership of all the files and directories that the server needs to use. Thanks jabaa.


  2. Being at the same folder level than the server app folder, is not the images folder "out of reach" for the server app or, at least, requiring a different path to reach (../Images/)?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search