DEV Community

Dimitris Chitas
Dimitris Chitas

Posted on • Edited on

JS Node (express-rate-limiter)

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

I will use only one js file which we will call it app.js
So i move toward to the folder path

image

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

image

and finally we open the editor(VS)

image

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

image

So next we open our terminal again and we type

image

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

image

After that we type again to the console.
image

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

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

After our server creation we include the limiter package in that kind of way.

image

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

image

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

And use it like this.
image

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..

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
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/

Top comments (2)

Collapse
 
liyanacarr profile image
LiyanaCarr

Thanks for sharing. Actually, I am good at Python and nowadays, I am thinking to start learning Node JS. It would be great if you suggest any free place to learn it.

Collapse
 
feco2019 profile image
Dimitris Chitas

Hello there,i am glad that you found it handy.I would suggest you Traversy Media crash course for node and then i suggest you the documentation for sure and maybe some udemy courses make it more clear to you.Python is also very dynamic language and along the trends.Best Regards