Static files and Public folder

Hello, I have a question. I know that static files are files that aren't going to change in our back-end, like css files. Now, the purpose of the public folder is to "host" these static files so that anyone can access them? In express.js, we explicitly need to use a middleware to do that, I didn't understand why though, I mean does this bring? Automatic MIME types?
3 Replies
13eck
13eck3mo ago
Express uses middleware because otherwise you'd have to do everything yourself. Read file. Verify file. Figure out file type. Make correct headers. Send header. Send file. Close connection. All that (and more!) is done via Express. Although you should be using a reverse proxy like Caddy or nginx to handle the static files b/c it'll be better than anything you can do in JS. It's battle-tested. And bonus, it allows you to proxy HTTPS to HTTP! So your API, for example, has a GET /user?id=38 route, right? Instead of needing to spin up an HTTPS server in Node you do an easier HTTP server and your reverse proxy handles the TLS connection and forwards the needed info to your HTTP server. Your Node HTTP server then sends the response back to the reverse proxy who then forwards it to the client. Less work for you and a much more robust and stable archetecture.
Faker
FakerOP3mo ago
Yeah I see, I have a question though, say we build an app where we have the option of uploading images. Those images go onto a folder. From what I've been told/read, normally, this should go into the cloud. But my question is, if it's in the cloud, it won't be a problem for MIME types? Like since it's not our public folder serving the files?
13eck
13eck3mo ago
Wherever you host your images will give you a URL. You use the URL in the img tag. HTTP handles the rest

Did you find this page helpful?