FCC Speedrun — File Metadata Microservice
Next in line as I work through the backend projects in order to finish the Chingu FCC Speedrun Challenge speed run is the File Metadata Microservice.
I know you’re going to think I’m crazy — but I’m just going to say it anyway — this one is EASY. Yep, I went there, it’s easy. Do you want a couple tips that will make this one a walk in the park?
Read the documentation for multer.
Really, it’s that simple, this project is pretty much a direct copy of the project you created for the timestamp microservice, all you have to do is add the “multer middleware” into your post request.
Here’s the couple bits you’re going to want to pay attention to:
upload.single
This is the middleware you want to add in right before you ship your post request off to your module that handles the request.
Note that this snippet looks much like the routes I’ve been showing snippets for in the prior microservice projects, the only difference is the upload.single call right before I send it off to my fileMulter module. (So no one gets caught off guard, the parameter you pass to upload single is simply the name of the form field that corresponds to your upload input on your input page).
router.route('/api/file-size').post(upload.single('file'),fileMulter.parse);
MemoryStorage
You want to use Memory Storage so you don’t have to actually store the file that’s being uploaded on your site.
The memory storage engine stores the files in memory as
Buffer
objects. It doesn't have any options.var storage = multer.memoryStorage()
var upload = multer({ storage: storage })
If you get those two things down (upload.single and Memory Storage), getting the file size is a one liner because the size of the file in the request object is simply req.file.size.
Notes:
- You can view the source code here
- You can see both the code and a live demo on GoMix.
- You can find Francesco Agnoletto (Kornil’s) amazing repository of favicons here.
My progress in the Speedrun thusfar:
- Tribute Page
- Random Quote Machine
- Timestamp Microservice
- Request Header Microservice
- File Metadata Microservice — this post