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

December 5, 2018 654 views
Block Storage Node.js Ubuntu

Hi,

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!

1 Answer

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

Jarland

Have another answer? Share your knowledge.