简体   繁体   中英

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. 在此处输入图像描述

im using this code for upload and its ok在此处输入图像描述

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

http://localhost:8000/../../fileArchive/1661839542935/name.jpg

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 ../../ in your path. 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
app.use(express.static('../fileArchive'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM