How can I use DO block storage as static folder in express app?


I can’t seem to find an existing answer to my problem, so I hope someone can help me here:

I have an app with express backend where users can upload images that show up on their profile page. Right now (in development) all files are served by express through app.use('/public', express.static(__dirname + '/public'))

Now I decided on DO for deployment (which btw ran quite smoothly using dokku, thanks for that!) and wanted to ask if there is a simple way to have express serve (and upload) the files from (and to) DO block storage - so that the front end is still able to access all files from /public/* That would be amazingly helpful.

(btw, the uploaded images get resized and saved in 3 versions each and the therefor existing folder structure is why I thought it was better to go with block storage vs. object storage - but I would be also open for suggestions on how to make this work using object storage)

Thanks in advance and sorry if I’m missing something obvious!

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hey friend,

The easiest way to think about this is to extract the basics of this out from the complexities. Forget about the stack and instead just focus on block storage being a mounted directory in your Linux system. What that directory contains and where it is are entirely up to you.

Let’s say, for example, your app exists here: /home/app

And then let’s say that public folder is here: /home/app/public

At that point, let’s just mount /home as block storage: (WARNING: The first line of this will wipe the block storage volume, don’t do this if you have data on it)

mkfs.ext4 /dev/sda
mv /home /home2
mkdir /home
mount /dev/sda /home
mv /home2/* /home
echo "/dev/sda /home ext4 defaults 1 2" >> /etc/fstab

What I’ve just done:

  • Formatted the block storage volume
  • Moved your home folder to the side
  • Created a new home folder
  • Mounted the block storage volume as /home
  • Moved the old items from /home into the new volume
  • Added the volume to fstab so it mounts on reboot