Reputation: 1619
I have 3 servers running as a cluster managed by flynn (a bit like heroku) which I can attach a shared drive to so that in theory they all have access to the same files.
However I'm a bit unsure if it's possible to access a mounted drive from my nodejs app to save and access files.
Can anyone shed some light on if this is possible and roughly how I'd go about doing it.
Upvotes: 2
Views: 3493
Reputation: 707198
With node.js, your file system path has literally nothing to do with the URLs that your server supports. node.js servers do not serve ANY files by default (unlike some other servers).
If you want an incoming browser request for http://example.com/uploads/test.jpg
to read a file from /mnt/shared/uploads
, then you have to create a web server route handler that includes the incoming path http://example.com/uploads/test.jpg
and then reads the data from /mnt/shared/uploads
and writes that data out as the http response.
Depending upon what web server environment you are using, there are helpers to do that mapping. For example, express has express.static()
that helps with some auto mapping. But, the web server by itself does not server any files like this automatically.
So, if what you want is that all incoming requests for http://example.com/uploads/*
will be read from /mnt/shared/uploads/*
, then you can use express.static()
to help you do that like this:
app.use("/uploads", express.static("/mnt/shared/uploads"));
This will take any path it finds after /uploads
and look for that path in /mnt/shared/uploads
. If found, it will automatically serve the content as static content.
So, it would work like this with the incoming URL shown first and the place express.static()
would look for a matching file:
/uploads/test.jpg ==> /mnt/shared/uploads/test.jpg
/uploads/bob/test.txt ==> /mnt/shared/uploads/bob/test.txt
Upvotes: 2