I have a lot of goals currently including but not limited to: getting a better job, creating more content, and creating interesting projects. In regards to the second goal I’ve written a few articles over the past year. The articles mostly describe projects I’ve built and how I built them, and they don’t do that very well If you ask me. After a lot of thought I’ve come to the conclusion that what I need is practice. In the pursuit of practice I am trying to write less about in depth technicality and more about abstract concepts and educational tutorials.
So here’s what I learned last week:
Warning, In the following paragraphs I expose myself for being embarrassingly inexperienced lol.
How to test express with Mocha and Chai
This week I began working on my first serious foray into server-side JavaScript via a habit tracking app. For the front-end I’m planning on using React or maybe Vue if I feel like learning something new. For the back-end I’m using Express with MongoDB and mongoose.js as an ODM. On the whole I feel extremely comfortable with JavaScript but coming from a Ruby on Rails background, I found myself writing lower-level code than I am used to.
For example; mongoose has no equivalent to rails’ cascading delete via “dependent: destroy”. As a result I found myself flipping back and forth between several tabs in postman and vscode and the mongoose documentation to create and recreate multiple documents while trying to work out how mongoose pre and post hooks could be used to trigger the required subsequent deletions. Needless to say the necessity of testing was evident.
A quick google search brought me to two wonderfully named packages [Mocha]() and [Chai](). Mocha is the testing framework that runs the test files and Chai provides better assertions, though Mocha can be used on it’s own, the Chai assertion scheme is nice and reminds me of [rspec]().
setup is easy simply install the packages
npm install — save-dev mocha chai
create a folder for tests
mkdir test
add a testing script to your package.json
{
“scripts”: [
“start”: “node ./bin/www”,
“test”: “mocha test”,
]
}
next create a folder for your tests and test
mkdir test && touch test/app.spec.js
Write tests within app.spec.js and they’ll run when you use
npm run test
Testing is my single greatest source of resistance and I am always striving to incorporate testing into my workflow.
vscode’s built in Node debugger
To piggyback on the last topic in the context of coming from a Ruby on Rails background. When working on the back end I was accustomed to using pry to debug my code. A quick google search for “JavaScript pry equivalent” returned nothing of interest. I was used to the simplicity of in-browser debugging for JavaScript and I wanted something like that for server-side. I was aware that vscode had debugging capabilities but I’d always assumed they were for c or something like that and wow was I wrong, It turns out stock vscode can only debug Node and it’s very easy to use.
I had chalked up my slow going development to learning express and my unfamiliarity with server side JavaScript and that conclusion was mostly correct however I find myself moving from big bug to big bug much faster now haha.
The value of async await
While I am by no means a stranger to the use of async await, my current project has turn me borderline evangelical. When using mongoose in the controller actions async await is a necessity for readability and can reduce the number of lines of code by at least half if not two thirds in most cases.
How to use Obsidian
I recently posted and article about why I switched from Notion to Obsidian.md and I will say the time I’ve spent using Obsidian since publishing that article has only galvanized my belief in the thesis of that article:
“Restriction sparks creativity”.
I would even expand on that thesis to say that I believe too many possibilities or (features) creates a mental block due to the need to make decisions. Over the past week I have found myself to be more organized and better able to create things.
How to simplify express routes by using controllers
I moved my route logic from the routers themselves to separate controllers. I’m a big fan of short, concise code without too many bells and whistles. The simplicity of a router that just calls functions is pure beauty.
My goal of creating a Saas product
I’m a fan of hackernews and through my daily perusal I’ve come across several threads of people discussing their Saas business failures, successes and journeys. I find it incredibly inspiring, not in the get rich 10 Lamborghinis in my Lamborghini account quick kind of way, but I would love to create something that provides enough value to people’s lives that they are happy to spend their hard earned money on it.
My main barrier is ideas, I feel confident that I can code anything up but I need Ideas. My plan is to research the workflows of niche occupations and figure out what I can reduce, simplify or automate.
What I want to learn next
1. I still haven’t exactly figured out how to implement cascading deletion / reference cleanup with mongoose though I feel as if I am on the cusp of getting it.
2. Over the next week I want to be comfortable with Mocha and Chai.
3. I want to take my understanding of git to the next level.
4. User Auth in express
Conclusion
Overall I feel good about what I learned and accomplished this week and I am excited to see what next week brings.
If you enjoyed my writing feel free to check out my website and connect with me on LinkedIn