How to store data with nodejs outside of the project folder?


I am using ExpressJs and i want to save my files outside of the project like this.
enter image description here

im using this code for upload and its ok
enter image description here

my files uploaded correctly but i can not show them in the app cause route will be like this


nut when i set this to src of an image, the src doesnt show my image


This is because express is trying to protect your project files from getting exposed on a malicious request. By default you cant access any of the files in your project or other directories. You can define a directory that can be accessed using the express.static method.

This will define a directory as a static directory which can be accessed (see express.static). After you added this you can also drop the any path in your URL. Express will check for any files matching the name in one of your static directories.

// This would allow the user to access the files in that directory
// ../fileArchive will be considered as the fileRoot e.g. localhost:8000/test.txt would look for a test.txt file in ../fileArchive

// if you want to keep fileArchive as part of the url you can do that like so 
// however I would recommend to exclude the `..` to keep the url readable and also the exact file path on the server should not concern the user just where he can find it
app.use('/fileArchive', express.static('../fileArchive'))

Answered By – Vincent Menzel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published