CORS CORS CORS

The project plan is approved and my tasks are fixed. I am now the owner of AWESOME APIs. My job was to supply my team-mates with APIs so that they can take care of the cosmetic UI and I deal with APIs and backend hosting. Off we go!

Learning how to host REST APIs on node was the first step. The 2nd was having it interact a datastore. Thankfully all these topics come with standard npm libraries. I was able to quickly create a console application to prove out these concepts.

Very quickly, with a standard reference template found on the web, I was able to create RESTful functions. Router.get, Router.patch, Router.post, etc. All these were pretty intuitive for me.

After that, I have to learn all about the http status code and what to return depending on the context. It was a lot of details! Some times I do struggle with the temptation of just returning a generic 400 error code, but the perfectionist nature in me pushed me on. I carefully created the error codes and was pretty satisfied.

Now it’s time to complete the end to end REST API. It should have have an example of how to interact with the API along with some test-cases. So i dutifully created a postman suite and test-cases and went on the deploy the application on GCP. (more on this in my next blog post). Everything is working!!

At this point, I was feeling really good. I was ahead of schedule and I got the momentum going for me. I felt that I could write all the APIs in 1-2 days. It was a good feeling! So I announced proudly to my team-mates on the teams chat that they are free to use the awesome APIs that I have created.

After a long wait, my teams chat started to beep constantly with “Hey I am getting a CORS error”. “Hey, it’s not working”. “Are you sure it’s working?”

My feel good period was short-lived and I spent the next few hours into the night trying to make the API work for my team-mates. And it was all down to this 1 line of code that I missed out. (I never had experience with CORs issues. Postman always worked)

Amazing, how 8 hours of googling and research led to this 4 lines of code. It was great again! I learned something too about CORS.

Leave a comment

Your email address will not be published. Required fields are marked *