File.io is a popular file service for hosting and serving ephemeral files. The home page looks like this:
A fairly simple home page with an option to upload files. While file.io allows multi-file & folder uploads, we'll stick to a single file. Again, we don't want to increase scope too much and make it overwhelming.
The single upload file button opens up a dialog to choose files:
Once upload is complete, we get a unique URL for the file and a QR code that can be shared.
That's all about the upload part. Coming to the download part. We can use the file link anytime to download the file (exactly once). If the link is valid, the file details are shown with an option to download it. As soon as the file gets downloaded, the server removes the file.
If the link is opened next time, we'll get an error:
We're going to go over a very similar service that has been written using vanilla JS & express server in the backend.
File.io service in vanilla JS
The code for the file.io service in vanilla JS is available in a GitHub repo. The repo can be cloned using the following command:
Once the repo is cloned, go to the folder vanilla-app and start the server:
$cdvanilla-app$npmstartvanilla@1.0.0startnode--watchserver.mjs(node:38294) ExperimentalWarning:Watchmodeisanexperimentalfeatureandmightchangeatanytime (Use node--trace-warnings...toshowwherethewarningwascreated) File.io vanilla app listening on port 3000
Once the server is up, open the browser and go to http://localhost:3000:
The interface is pretty simple, just like the real file.io service (minus the ads, of course). There is a single upload button that can be used to upload any file.
Pretty good for our simple app. Now, let's open the link provided to download the file. When the link is opened, it'll show the file details and a download button. As soon as the download button is used, the file will be removed from the server. The page in the back will refresh to 'file doesn't exists' as soon as download button is used.
Again, pretty good for our simple vanilla app. Now, let's dig a bit deep in the code for this fake file.io service. The folder structure is very simple:
Server code
The server code is present in a single server.mjs file. This contains the express app, static file server, file upload/download APIs & encryption/decryption logic. Everything in one file. The only reason to put everything in one file is tha,t this book is about learning React, not server-side programming. A single file keeps it simple for us. One thing to note that, the uploaded file is encrypted before writing to disk (just like file.io). The same file is decrypted & removed before it gets served. This make our file.io service very similar to the real one.
For the UI side code, there are two HTML files, three JS files, and one CSS file. For helping with the UI work, we've used bootstrap, QR generator and sweet alert.
upload.html
This contains everything that you see on the upload page.
In the upload.js file, we can see there is a lot of code around showing/hiding elements, adding/removing classes, setting/removing values from elements. This is pretty usual for a vanilla JS app. If the same app is written using jQuery, the only difference would be usage, and nothing else. All the additional helpers come from utils.js.
download.html
This contains everything that you see on the download page.
The code download.js is very similar to the code in upload.js. There is a good amount of element visibility & content manipulation. There is even a piece of code to create the file download popup when the 'download file' button is pressed. This requires creating a dynamic element 'a' with download option.
utils.js
The utils.js file is common for upload and download. It contains utility function for manipulating the elements.
That's all about the file.io vanilla app. The app code should look familiar to you. That's the basic premise of this book. You're coming from a vanilla JS or jQuery background and are interested in learning React.
Before moving ahead to the next section, you should take a pause here and do the following activities:
Clone the repo
Run the server and test the app some times
Go through the code in detail. Understand the HTML and JS code (every line).
Again, nothing in the vanilla app should give you any trouble. This is your day-to-day job after all.
Again, it's best to pause here and follow the exercise.
--
This is the conclusion of part 1 of this book. The next part covers the basics of React starting with setting up a local development environment.