Building A RESTful API With NodeJs And TypeScript

Gift Banda
4 min readOct 12, 2021

Hi, In this article (or series of articles), we will solve an Engineering Code Challenge by building a RESTful API that simulates how a Vending Machine works. Enjoy!šŸ˜‰

Challenge: Engineer a program in the form of a Web App.

  • A vending machine that vends products based upon four (4) denominations of coins (50 Cent, 1 Rand, 2 Rand and 5 Rand) returns coins if there are no products.
  • When the application loads, it loads data about all the instances from the products and Coins and visualizes it somehow. (e.g. A table with all the data).
  • The application must have some mechanism for refreshing all the data from the API without reloading the entire application (e.g. A refresh data button).
  • The application must have some mechanism for refreshing the data of only a section (products or coins).
  • The application should show the user some indication that it is refreshing/loading new data.

Letā€™s go!šŸš€ We now know a bit of what is required for our solution to meet the requirement. First, we will focus on the part of the application that does the heavyweight processing of data, the calculations, handling of the incoming requests, and sending out responses. To do this, we are going to build an API using NodeJs, Express and TypeScript.

How API works.

API stands for Application Programming Interface, a set of definitions and protocols for building and integrating application software. An API will let our service/system communicate with other services/systems without knowing how they are implemented. This can simplify app development, save time and money ā€” APIs give you flexibility, streamline design, administration, and use, and provide opportunities for innovation. This means that we can build the part of the application that does the data processing (back-end), and have it fully working, doing its job, and then have a different system like a front-end application communicate to our back-end application through this API, cool right!šŸ‘šŸ½

Getting To Know Our Tools And languages -

As mentioned above, we will build an API. We will be using these tools/languages: NodeJs, Express and TypeScript.

NodeJs is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project!

Express is a fast, unopinionated, minimalist web framework for Node.js (read What Is a Framework in Programming and Why Is Useful here)

TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

  • It brings a ā€œstrictā€ type system to JavaScript.
  • It makes our code more robust and less prone to errors.
  • It gives our code OOP capability (classes, interfaces, generics, modules and more).
  • Itā€™s great for larger projects.

Installing Our Tools And Languages -

Navigate to the NodeJs site https://nodejs.dev/download, follow the instruction there to download and install NodeJs into your machine, then verify that the NodeJs has successfully installed by running the following command node -v on your terminal or shell, then press the enter key, which should print out the NodeJsā€™ version that has been installed.

At the time of writing this, I had the v14.16.0 Node version installed on my Mac. You might have a higher one than mine; itā€™s still good. However, if you got a different output that is not in the format of the node version, it probably means your installation was unsuccessful. At this point, I recommend that you go and google how to install Nodejs to get more accurate and precise instructions on how to have it installed.

TypeScript, having installed NodeJs successfully, now install TypeScript globally so that we can access it from any directory. Run the following command.

npm install -g typescript

Note: you may have to use sudo on Mac if you donā€™t have permission or run as an administrator on a Windows computer.

We go back to our terminal and run the command tsc -v to verify if that installation was successful. You should have an output similar to Version 2.2.1 , but if you have a higher/latest version, Itā€™s perfectly fine, and if your installation was unsuccessful, then run back to google for a detailed installation guide.

We also need Visual Studio Code, VS Code is an awesome lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and NodeJs. Exactly what we need! Follow the onscreen instruction on their site https://code.visualstudio.com/ and have that installed as well.

All Good To Go šŸ’Æ

We have now installed everything we need at this stage. The other stuff like Express we will install when we start our actual coding/development of the API. Follow the link below to continue with the article.

If youā€™ve enjoyed this, then a ā€œFollowā€ and/or a ā€œSubscribeā€ would be great. Thank you!šŸ™ŒšŸ¾

--

--

Gift Banda

Software Developer | The CloudšŸŒ„ | Tech-Enthusiast | Bible Scholarā¤ļø | https://giftmbanda.com/