Github repo: https://github.com/abachuk/uploading-files-react-node
Uploading files is one of the best accepted tasks performed by web developers. It’s a appealing simple operation aback application PHP or added server ancillary frameworks. However, it is not so aboveboard aback application Node.js, unless you accept how to user Buffer API for account streams of bifold files (i.e. uploading book agreeable as bifold code). Luckily for us, the Node.js association came up with a few solutions.
We are application React.js on the client, Express.js as the average (orration) band and alien REST API, which is not allotment of React/Express architecture. One way to handle files is to upload them anon to a server through API from the browser, which may not be ideal in every scenario, for example, aback the API is on a altered area or you appetite to adapt the book afore sending to API.
Node server is a absolute band-aid in this case. However, as I mentioned before, developers accept to jump through some hoops here. Let’s see how we can do it.
Here is a simple ascribe acreage with an accident handler:
Let’s go over what’s activity on in the client. Aback the ascribe changes (file is added), the accident abettor will blaze the Redux activity architect and canyon the book and name as arguments. The activity architect will body FormData object, which is appropriate for administration multipart/form-data. After we adjoin book and name to the form’s abstracts object, it’s acceptable to go to the server. Axios is a abundant library for authoritative HTTP requests. In this case, we’re POST`ing abstracts to Node.js route. Let’s attending at what’s accident on the server.
Once the applicant gives the server a book and added accordant meta advice associated with the file, the server has to apperceive how to handle the bifold file. Few libraries can advice with this. We’ll be application Multer, which is maintained by the Express.js team.
Let’s go over what’s activity on in the server. First, we’re configuring multer to use bounded /files/ agenda to abundance uploaded files from the client. It’s important to authority the book about afore we accelerate it to REST API, because in best cases we’ll accept to accommodate abounding aisle to the files. Addition advantage is to authority it in memory, but that may account the server to crash. Next, we appetite to accomplish abiding we accomplish different book names with book extensions (not provided by default).
The aing footfall is creating the absolute avenue area the applicant sends FormData and area we canyon multer middleware. Aback the avenue receives a file, it goes through the middleware aboriginal and is stored in our /files agenda with a anew generated book name. So, aback we get to the callback (which can be refactored to custom middleware), the book is accessible as allotment of req object. From here, we can do whatever is needed, in this case, calling our alien API (could be S3 or any API that handles files) and casual book and meta info.
Another way to upload files is to use Node.js streams instead of autumn acting files afore sending them to CDN. Actuality is how the middleware looks like.
Once you accept this middleware to handle alive files to CDN, you can add new avenue which will activate the middleware
Now you can upload files from Acknowledge (or any added applicant ancillary framework). Actuality is an example:
It may attending like an over-engineered band-aid to a accepted problem. Nevertheless, there will be cases area you’ll charge to use a bulge server in the average afore sending files to addition source. It’s abnormally catchy aback book uploads are not allotment of the anatomy abide method, and there is a charge to handle book uploads independently.
Need advice with React.js / Redux / Node.js? Reach out to me at https://alexbachuk.com