How to create your own API Gateway from scratch

 

What is an API Gateway?

An API Gateway is critical component of modern design, particularly in microservices-based applications, because it provides a layer of abstraction that facilitates client-underlying service interaction. It improves the maintainability of distributed systems, scalability and security.

How to create it?

Summarizing all the steps we will be taking for creating API Gateway:

Step 1 : Install Dependencies

Step 2 : Request parsing

Step 3 : Authenticate the request

Step 4 : Configuration file

Step 5 : Resolve the request

Step 6 : Request forwarding

Step 7 : Deliver response

In this article we will see how to build our own simple API gateway from scratch. Basically API gateway is just a centralized place for requests to be made to your APIs, so instead of consumers or users making requests directly to your APIs, they make it to API gateway and the gateway job is to handle those requests and send them to the correct APIs and get the responses back to the sender. So, we will begin with making a server or setting up the server and then from there we will just going to create the gateway. So let's start, we will be using Node.js, Visual Studio Code, Express and Axios in this tutorial. Let's begin:

Run following command in the VS Code Terminal.

npm init -y



This command will setup our node.js project and once we have that done, we need to install some dependencies. We are going to install Nodemon, so for that run following command in terminal.

npm i -D nodemon 


We used -D because this is going to be a development package. Basically what nodemon is going to do for us is whenever we make changes to our server we don't want have to keep stopping it and then restarting it to update those changes so nodemon is just like a live server it will update the changes as we go. Also we need to install express and axios. Express is what we use to handle our requests and is pretty much a server thing and also we need axios, so axios is going to handle our HTTP requests. So let's run following command for that and hit enter in terminal and just let those install.

npm i express axios


Once those are installed we can begin our programming so first create a file called gateway.js, you can use following bash command in terminal.

touch gateway.js


Open gateway.js and this file is gonna be our main app so here we will establish like the actual starting of the server and everything like that. So we need to establish an express variable, then create our app. Then we need to set express to use json and then we need to start our server for that we will create a port variable which is basically at what port our server will be running, lets set it 4000. And 'app.listen' also takes a callback that we will use just to printout that the server has started. Write following code in gateway.js for doing all this:

An error was thrown.

Now we should be able to start up our server but before we do that let's create a script to run this server, so in our package.json file, under the script tag we will be creating dev script because whenever this script will run then it will run nodemon and it will call our gateway.js to start up our server. For that just make following changes in script tag of package.json and kindly don't change any other code of this configuration (package.json) file. Write following code:

"scripts": {
"dev": 'nodemon gateway.js",
"test": "echo \"Error: no test specified\" && exit 1"
}


Make sure you saved the file after making changes. In order to run this, type following bash command in terminal:

npm run dev


As we can see that our console.log has printed out that means we are running on port 4000. Now we can begin creating our process for handling the requests that come in and going out to APIs. So for that we will create an endpoint and we would do this by app.use method which is going to take a path "/" and then it will take routes to handle request so we first need to create const for routes. For this we will make following changes in gateway.js file:

An error was thrown.

Whenever we send a request to this server on port 4000 with just a forward slash(/) it will use this routes file that we are about to create. So let's create a new folder routes and inside this folder we will create a file called index.js. In this index.js we will write code for our routes. The reason why we can just call dot slash routes('./routes') instead of ('./routes/index.js') is because its going to automatically look for this index.js file if we not specify it as it is the only file we have in this routes directory that's why we will do in this way = const routes = require('./routes').

Now in index.js we will establish our express variable in the same way we did in the gateway.js. Then we will add router which is actually route the HTTP request and export this router using following code in index.js.

An error was thrown.

Now, we can go ahead and create our first endpoint that we'll actually use, so we will use all() method that takes any of the http method type so its going to be get, post, delete, update, all of those will be taken in here. The path we are going to use is having a path variable in it so the path variable will be "apiName" so this is going to be the name of whichever api you are trying to call and then use a callback because second parameter takes a callback which is going to have the request(req) and response(res) objects in it and inside of this callback is where we handle all of the logic, so once we get this apiName we'll know which way to forward this request but as of now we don't have any api that are registered in any of that so what we're going to do is just return this apiName to make sure that everything is hooked up and working correctly, so the way we can do this is by using response object, we will call res.send and then in here we're going to get this apiName variable, so we're going to do that by req.params.apiName. So we will be sending this back whenever there is a request forwardslash(/). We can also log this on the server. For doing this add following lines of code in your index.js file:

An error was thrown.

We have created our own API gateway, now let's test it by calling it.

For that get a new terminal up by clicking + because our server is running from 1st terminal. So, this terminal we are going to be used to make call to this api gateway here, so the way we do this is we just using curl or you can use postman or anything like even your browser for making a call. But we will use curl and then call localhost on our port 4000 that we decided to run this on and then we could pass in anything here we're going to pass in hello gateway though and we should get it to comeback. For this run following command in 2nd terminal:

curl https://localhost:4000/hellogateway

Now see the results on the server side (1st terminal) we log this hello gateway which is from line 5 of index.js and then our caller side (2nd terminal) we got hello gateway back because that's what we sent back. So this is how we're going to handle requests coming in. Basically when we get a request we'll just check the api name and then forward that request with the same headers and everything to the api that's being called.

Comments

Popular posts from this blog

ETL

How to upload an R package to Azure Machine Learning