Stumbling Through Routes in Node.js

Abdul Aljebouri
Aug 28, 2017 · 2 min read

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:

  1. Get a mongodb instance up and running locally (https://scotch.io/tutorials/an-introduction-to-mongodb)
  2. 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*

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade