JS Node (express-rate-limiter)
Tags: node, express, javascript, backend
Published: 2021-02-11 16:34:40

Hello there guys today i will talk about  express(framework) and more specific for one of its package which is called rate-limiter.
It is a module that can handle malicious or incificient requests from outcoming/incoming traffic.

#What we need for  this project?

* Node Js 
* Express Js
* Express rate-limiter depedency
* Code  Editor(I am  using Vs-Code)
* NPM

So let's see in action  what excactly  rate-limiter does.

First we need to create our root folder and then let's 
create an express  server in Node to see how we can deploy that 
and what are they options we have.

I  am using gitBASH for my basic  foldering/sourcing cli activity,you can use one of your choice the commands are simular  you can do it manually as well in windows enviroment.

So first i am  creating my project folder called NodeLimiter

![image](https://dev-to-uploads.s3.amazonaws.com/i/75a6ud9pws22blltuncz.png)
 
I will use only one js  file which we will call  it app.js 
So i move toward to the folder path

![image](https://dev-to-uploads.s3.amazonaws.com/i/25pgpf3glagsbfddi35e.png)
 

And inside of the root file we create the app.js

![image](https://dev-to-uploads.s3.amazonaws.com/i/h3q35jytryhkfdeh5qid.png)
 
and finally we open the editor(VS)

![image](https://dev-to-uploads.s3.amazonaws.com/i/1e7pfc4wvlpyv76dazgn.png)

So we are here and now  it is the time to install our depedencies as  well  to initialize npm

![image](https://dev-to-uploads.s3.amazonaws.com/i/9fa171gx5qyv2gvcfm8k.png)
 
So next we open our terminal again and we type 

 ![image](https://dev-to-uploads.s3.amazonaws.com/i/bml1nlo0fnb43sjwmvws.png)

We press enter several  times and if we go  back at vs code we have created package.json file 

![image](https://dev-to-uploads.s3.amazonaws.com/i/d3vo6f5dqy7jwul2dm0f.png)

After that we type again to the console.
![image](https://dev-to-uploads.s3.amazonaws.com/i/vrsb7nqc4cqi7n10wsoi.png)
 
We press again enterand we have our package-lock.json file

Now let's install express and  express rate limiter again in the console we can type the name of the packages that we need with space between them

 ![image](https://dev-to-uploads.s3.amazonaws.com/i/iyneslhvocyetpp57hh3.png)
 
Nice we are ready to go!

First we need to create  our server and give some basic configuration so follow me.

As we finish with theserver, we initiate express and parse it to variable called app , then we define the port that we like to run our server and should look something like this 

![image](https://dev-to-uploads.s3.amazonaws.com/i/9zjihtuy8ztql1xdcdx5.png)
 
After our server creation we include the limiter package in that kind  of way.

![image](https://dev-to-uploads.s3.amazonaws.com/i/t325z4rmaagulh76hrb6.png)

Lets create now three routes and lets use our package to set some restrictions to them,how much request accept  and 

![image](https://dev-to-uploads.s3.amazonaws.com/i/w7b0xogpceys8073emli.png)
 
One root diretory, one about and one about with different request method.
All of them respond with the ok message except home which returns hello world.
Simple?

Now let's create our midlleware that will limitate the requests,as you can see in the picture above i have 2 GET requests and one POST so we will approach two different actions.

Let's create our variable called reqLimiter and we will call  the rateLimit method of express packaged that has the following.

![image](https://dev-to-uploads.s3.amazonaws.com/i/bo6n1z30mgwfncp0w601.png)

And use it like this.
![image](https://dev-to-uploads.s3.amazonaws.com/i/xrn3nwsmq2esdcsjw7qs.png)
 


 

#NOTE 
What parameters this method takes as object?

**windowMs** – This is the window size (24 hours in our case) in milliseconds
**max** – This represents the number of allowed requests per window per user
**message** – This specifies the response message users get when they have exceed the allowed limit
**headers** – This specifies whether the appropriate headers should be added to the response showing the enforced limit (X-RateLimit-Limit), current usage (X-RateLimit-Remaining), and time to wait before retrying (Retry-After) when the limit is reached

Better description about those properties you can find here
[MoreDetails..](https://blog.logrocket.com/rate-limiting-node-js/)

 Start your server,type on your terminal :
``` node app.js
``


Exist more properties that you set in the object but those are basic for our example the windowsMs defined for 7 mins, max number of requests are 5,headers are true and the message that returns after five request is parametric for the example.

So now if you open your browser at the local host
[LocalHost](localhost:4000) 
You will be able to see the home route and the Hello Word!

Try to refresh 5 times to see what you get.
You can also use POSTMAN for more advanched tests and different Request Methods.
Play with endpoints,reach more values for the limiter by different resource and that's it guys you created your first node express layer for bad requesting.

Have a nice workday guys, in case for further explanation do not hesitate to contact me or find me in github or linkedin.
GitHub : https://github.com/feco2019
Linkedin : https://www.linkedin.com/in/dimitris-chitas-930285191/