Stumbling Through Routes in Node.js
Now that I had a node server up an running, my next task was to get two APIs going (POST /users and GET /users/:id). Getting things started was a bit harder than I was expecting but I finally managed to get things going after two hours. There were two things that needed to be done:
- Get a mongodb instance up and running locally (https://scotch.io/tutorials/an-introduction-to-mongodb)
- Set up routes for an API (https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd)
I am using Postman to test my APIs. This is where I made my first mistake. In retrospect it was so silly. I forgot to flip text to JSON in Postman so that the node server can handle it correctly. For the longest time I was wondering why the body on the server side kept printing out as empty.

The second gotcha was an issue getting the parameters to print out in the middleware function. There is because Express does not know that the request will end up matching “/users/:id” and that there is an id parameter. My workaround for this was to have the middleware function log the url. The url contains the parameters and I can use that when debugging.
Overall, this has been a fun weekend. My next step will be to get my Swift iOS app to talk to Node.js server running locally. I am also starting to mull two concerns: First, getting my requests to be through https instead of http. Second, how will I handle authentication and authorization. Hopefully, both will be as easy what I have been doing so far. *fingers crossed*
