0

I'm working on a Nodejs project that I will run only on my local computer (OS: Windows 10) and serve images and videos from local directories in computer drives (for example: { c:..\ Or D:.... or E:.... }).

Note: This is my first project using node js.

I've searched for answers but none of them helped me.

What is the problem that I have?

The problem I have is that the app can't read images from local directories and I get this error from the console on google chrome:

Not allowed to load local resources: file:///D:/Media/movies/action/Point Blank/folder.jpg

How can I enable reading from local directories in express node js without the need to run chrome with disabling security commands?

I don't know what is the problem, is it because of the slash or backslash in the URL path? or is there something I need to add in app.js?

Some Code lines from the project:

// Image 

<img src="D:/Media/movies/action/Point Blank/folder.jpg" alt="">

//App.js:

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

const app = express();

// Servie Static files
app.use('/assets', express.static('assets'))


app.set('views', [
  path.join(__dirname, '../views'),
  path.join(__dirname, '../views/admin'),
  path.join(__dirname, '../views/admin/categories'),
  path.join(__dirname, '../views/admin/users'),
  path.join(__dirname, '../views/guest'),
  path.join(__dirname, '../views/guest/home'),
  path.join(__dirname, '../views/guest/categories')
]);
app.set('view engine', 'ejs');



app.listen(PORT, () => {
  console.log("app is running in mode:", process.env.NODE_ENV);
});


Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
Fateh Alrabeai
  • 660
  • 3
  • 17
  • 35
  • Does this answer your question? [Cannot open local file - Chrome: Not allowed to load local resource](https://stackoverflow.com/questions/39007243/cannot-open-local-file-chrome-not-allowed-to-load-local-resource) – Shivam Mar 28 '22 at 13:52
  • do you want to serve files from your project directory or from outside the project directory? – HexaCrop Mar 28 '22 at 18:32
  • I want to serve files from outsite the project. I have no problem with serving files from the project directory. I want to serve them from any local path. @Jithin – Fateh Alrabeai Mar 29 '22 at 16:06
  • hi, i didn't understand the use case of sending files outside the project folder. in your code the asset folder is marked as something that you want to share – HexaCrop Mar 30 '22 at 17:00
  • sharing files from the system outside of the project folder without proper permission is not something that is recommended or secure – HexaCrop Mar 30 '22 at 17:01
  • 1
    @Jithin Yes you're right. I came up with a solution in which I don't have to read images or files from outside the project folder. – Fateh Alrabeai May 16 '22 at 20:47
  • glad it worked out like that – HexaCrop May 17 '22 at 05:42

0 Answers0