Part 01_Build a RESTful API using Node.js Express , MongoDB and JWT for authorization from the ground up for Beginners🚀
By: Renuja De Costa
Tags: Mongodb, Expressjs, Nodejs, Node Js Tutorial, Rest Api

This article is the first in a series of articles where we are going to build a RESTful API from scratch. How important is this going to be? If you are getting into MERN/MEAN stack development or trying to lay your hands on backend development with node.js, this article is going to be super important and helpful. This article consists of the following:

REST API introduction Building the basic routing using Express.js

Oh , you are not a developer, programmer, someone who writes code with an understanding?? Wait !!! The beginning section of this article is for you. You might want to know what an API is at least because without your knowledge, you are using APIs on a daily basis. First lets see what an API is.

What is an API?

The most common and simplest explanation of an API could be given by using a weather app on your mobile phone.

The developer who made the mobile app has nothing to do with setting satellites above the atmosphere of the earth to monitor weather.

Its just the companies who made the satellite and set into space🚀, have made the information gathered by the satellite about the weather accessible to the developers. How have they made this accessible? It’s by using something called as an API ( Application Programming Interface).

An API can also be called as an Abstraction layer. Abstraction is simply a way of organizing the complexity of a system so that complicated actions can be handled in a simple way. The developer who built the mobile application knows nothing about how the satellite gets its job done. It’s none of his concern. But what matters to him is the data the satellite gives. The developer uses the data and displays it on a beautiful User Interface and provides with multiple functionalities.

You might want to stop here if you are not interested in learning how to code to build an API yourself. Its going to get a bit technical from this point on.

What is a REST API?

Now that you have a basic idea of what an API is, in web development specifically, an API often refers to the way in which we retrieve information from an online service. There are thousands and thousands of API’s out there. Some popular ones are the

google API’s to integrate Google Sign-In functionality to your application google maps API that lets you customize maps with your own content and imagery for display on web pages and mobile devices. GitHub API that lets you use the information of github users, information such as their repository names, the users information, etc Youtube API allows developers to access video statistics and Youtube channels data NASA provides a number of APIs

The list just keeps going on. So far we’ve talked about API’s and we are going to build a REST API. But what is a REST API? To begin with REST stands for Representational State Transfer.

Not all API’s are RESTful but all RESTful services are APIs’.

REST is a type of API and for an API to become RESTful it should meet with certain criteria/constraints. We are not going to talk about the constraints here as this series of articles are written aiming at building one. Just for the knowledge, if an API serves data in JSON or XML or any other format(but most commonly JSON) it is the expression used to define a RESTFul API but be sure to checkout the REST API constraints.

So a basic knowledge of API’s ,what makes an API RESTful and a basic knowledge of Node.js is expected. We’ll be going through Express.js from the very beginning so no knowledge of Express.js is expected to follow this tutorial.

One more important thing, we are going to use ES Modules instead of CommonJS modules so make sure a recent version of Node.js is installed. You could check your Node version by running node --version on your terminal.

Express.js

We could create the API just by using node without using any frameworks, but using Express gets the same job done much more easily with a less amount of code.

Express is called an unopinionated, minimalistic framework because, unlike most javascript frameworks such as Angular, React etc, there isn’t a standard workflow and express isn’t very complex.

I’ll be using the Visual studio code text editor and this is what we have right now, just an empty server.js file. And we are going to do this from scratch as promised..