Onboarding
Chenosis
POSTED 2 weeks ago
Want to build your own API ?

Here is how, in this article you will start from scratch scaffolding a new Node js application then you will create a express js server which will allow you to define your API endpoints and then you will test them using postman.

[A] Blog structure (Bullet points)

  • Prerequisites:  
 
To follow along with this article, you should have probably heard the terms API, Public API, 
or Web API, REST, SOAP, CRUD, before.  You will also need to have prior knowledge 
around Javascript and also have a basic understanding of what an API is. If you do have 
previous experience with JS but you haven’t used Node.js, don’t worry, you won’t have a 
hard time going through the steps in this article.

Also need to have Node.js installed and have a working node package manager. 

  • What you will build: 

 We are going to be building a RESTful API that will have 2 endpoints that will go through
all the CRUD methods. If you don’t know what RESTful  APIs are or what this term 
stands for, take a look at this brief definition and explanation below:

A RESTful API is an Application Programming Interface (API) 
that uses HTTP verbs like GET, PUT, POST, and DELETE to 
operate data. Also referred to as RESTful web services, RESTful 
APIs are based on the REpresentational State Transfer (REST) 
approach, an architectural style that enables developers to 
manipulate data.

  • Getting started:

This is where we go through the steps of setting the project up by creating the project
folder and the files that we will need to get our server running: server.js , package.json

  • Installing dependencies:

We need to install the dependencies that will allow us to get our server running and to do 
that we run the following command: 
npm install express body-parser cors morgan --save

* we put the --save flag to add the packages that we are installing into 
the package.json file


  • Server setup and configuration:

To configure the server, we need to do the following: 
  • Import our dependencies
  • Define our express app
  • Add morgann to log HTTP requests 
  • Use bodyParser module to parse JSON bodies into JavaScript object
  • Start the server and listen on port 3000


  • API endpoint definition and setup:
  • Get instance of express router
  • Add a Get method to the instance and return the string “Hello World!”
  • Add a Post method to the instance and make it return the body of the post
  • Register router instance to the express app using the use() function

  • API endpoint testing:

 To test our api server we need to use a program like postman that we will use to make 
http requests. To see if everything is working properly, we will start with the GET 
operation first and then we will move on to the post operation. 
You can also use the console to view the http requests that are coming in from postman.




Refs:
https://auth0.com/blog/node-js-and-express-tutorial-building-and-securing-restful-apis/
https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4
https://www.toptal.com/nodejs/secure-rest-api-in-nodejs

[W] Blog Content

How to Build your First API
 
<subtitle> A quick guide for developers who want to start from scratch
 
Ready to build your own API? You’ve come to the right place! This guide will cover the scaffolding of a new Node Js application, the creation of an express Js server to define your API endpoints and the testing of your work in Postman.
 
What you need to know first
 
Before you begin, you’ll need an understanding of these terms:
API
Public API or Web API
REST
SOAP
CRUD
 
You’re also going to need to know your way around Javascript but don’t worry if you haven’t used Node Js before. Just make sure it’s installed and you have a working node package manager.
 
What we’re building
 
A RESTful API is an Application Programming Interface (API) that uses HTTP verbs like GET, PUT, POST and DELETE to operate data. Also referred to as RESTful web services, RESTful APIs are based on the REpresentational State Transfer (REST) approach, an architectural style that enables developers to manipulate data. (source: auth0)
 
The RESTful API we will build will have 2 endpoints that will go through all the CRUD methods. The fun begins with setting up the project in a few easy steps…
 
1. Installing Dependencies
 
We start by making sure that all the parts that rely on each other are set up to run like a well-oiled machine. This means installing the dependencies that will allow us to get our server running. To do so, run this command:
 
npm install express body-parser cors morgan –save
 
We include the --save flag to add the packages we are installing into the package.json file.
 
2. Server Setup and Configuration
 
To configure your server, follow these steps:
 
-    Import our dependencies
-    Define our express app
-    Add morgan log to HTTP requests
-    Use bodyParser module to parse JSON bodies into JavaScrpt object
-    Start the server and listen on port 3000
 
3. API Endpoint Definition and Setup
 
To set up your API endpoints, follow these steps:
 
-    Get instance of express router
-    Add a Get method to the instance and return the string “Hello World!”
-    Add a Post method to the instance and make it return the body of the post
-    Register router instance to the express app with the use() function
 
4. API Endpoint Testing
 
You’ll need a program like Postman to test your API server, because we’ll use it to make  http requests. Start with the GET operation and then move to the post operation in order to make sure everything is working properly.
You can also use the console to view the http requests that are coming in from Postman.