URL Shortener Microservice (FCC Speedrun Project #8)
How I finally gave myself a crash course in Mongodb.
So.. after 4 days of work on this project, my progress on the FCC Speedrun has slowed to something more reminiscent of a crawl. I’m still moving forward, though — slow and steady!
I started the URL Shortener project at a pretty good clip. The first steps — setting up a server, basic routing, etc — weren’t too different from the other API projects. Then I got to the database (storage!) part of the project. At which point I tripped over my own two feet, face-planted, and then tried to pretend like I did it all on purpose.
Tortoises can tell you more about the road than hares.
— Khalil Gibran
The good news? I understand more today than I understood 4 days ago. I’ve made it past this hurdle, and it will be that much easier to push through the rough patch I encounter. And — I finished!
Reflections
Installing MongoDB:
Obviously, the big “new thing” for this project was installing, figuring out, and actually using MongoDB. The installation guide in the documentation is good — but it does assume that you know at least a little bit about how computers work. Or at least about how your computer works.
In my case, I installed MongoDB (as instructed), then spent a slightly ridiculous amount of time finding the program, figuring out how to run it, and then figuring out how to run it from somewhere other than the program files folder on my computer.
Probably most people can figure out those first two steps on their own. For the third step… if you’re installing MongoDB on Windows, here’s how you can set the environmental variables to run Mongo (or other programs) from your own user “path”:
You’ll need to go into the Control Panel, and get to the environment variable for your user path. Once you get into the “path”, you’ll want to add (NOT replace) the path to wherever MongoDB was installed. For me, that was C:\Program Files\MongoDB\Server\3.4\bin
.
I did not figure this out on my own: Thank You to the folks in my Chingu Cohort who helped out when I got stuck!
Promises (again):
I’m really glad that I took time to work with JavaScript Promises during the last few projects, because they were a life-saver in this one. I started out doing database queries the “normal” way — that is, with callbacks. When that got unwieldy, I rewrote my (still broken) query functions to return promises:
That made my code easier to negotiate and made a big difference in helping me isolate and identify mistakes. Of which there were many.
And then I found the documentation for the NodeJS driver for MongoDB, and learned that database query functions can (optionally) return promises. Right out of the box. You can go the “normal” route and use callbacks. Or, if you omit the callback, the function will return a promise.
So I re-rewrote my query functions. And tried to convince myself that I was perfectly okay with all the extra practice I’d gotten rewriting functions:
Final Thoughts
After that, most of the frustrations (and there were many!) had to do with learning how MongoDB works, and learning how to unbreak all the things that I broke in the process. My code still isn’t pretty — but it’s organized, it’s commented to explain what it does (and why), and I have to start somewhere.
So I’ll just keep plodding along… keep moving forward. And who knows? Maybe I’ll even find myself improving by leaps and bounds.
(Code for this project is available HERE.)
Next up: Tic-Tac-Toe