Site icon apilayer Blog

Building a REST API with Node and Express

Node

Imagine you already have a working site. Let’s say your site starts to become more popular over time. If you build your site without thinking it will be popular later on, you will start to respond more slowly to requests. Active users will wait for you to reply to the previous user.

At this point, let’s start to explain exactly what Node.js is, how it works, for what purposes it is used and by whom it was developed.

Node.js was created to develop and run networked JavaScript applications on the server side. It is a working environment that enables the v8 JavaScript engine developed by Google for browsers to work outside the browser. The image that NodeJs creates in the minds is that it is designed for backend applications that only run JavaScript. The mission that Node.js wants to undertake is to provide structures that can be used in all software development stages, both to developers and companies aiming to scale, and to provide modularity.

How Node.js Works

Based on the low load, we can easily see that the working performance of Node.js is high. Keeping your system under load is essential when you want to write a scalable backend system. It is very necessary in terms of CPU usage to always run systems distributed on relatively low-capacity machines, scaled horizontally, at low load at all times. Node.js works very well at such low loads. Besides, Node.js uses a single thread to process incoming requests.

In this way, a new thread will not be opened for each incoming request. In this way, when too many http requests reach the machine in a short time, a thread will not be opened for each request, so a huge CPU overrun will not occur in Node.js and the machine will not be able to respond easily. Any CPU overrun will occur if there are too many requests for our machine and Node.js to handle. We assume that the spike part here will be on logical scales. In addition, by writing codes that can work asynchronously as a feature of Javascript, we can ensure that long-lasting processes are carried out from other threads. The unblockable input output loop in Node.js’ logic makes our http mechanisms work faster, while it uses the event loop and Javascript’s asynchronous operation logic to eliminate clumsiness from other requests. In this way, the processing and completion of a request in our system will not affect the processing of the other request.

What is Express.js

Express.js module / package is a Node.js based web application server framework. Building a robust API is quick and easy, thanks to the unlimited HTTP utilities and layers that Express.js offers.

Express.js is also one of the components of the MEAN software bundle. MEAN (MongoDB, Express.js, Angular.js and Node.js) is a free and open source software bundle used to develop web applications and dynamic websites. Also, Express.js is part of many frameworks besides MEAN. For the list, you can take a look at the list titled Frameworks built on Express1.

Advantages of Express.js Module

REST API with Node and Express

To develop a REST API with Node.js, Node.js must be installed on your computer. Assuming you have this setup, let’s continue with our article.

First of all, we create the folder where we will make the application with the following command from the terminal.

Then we enter the file we created and create npm. We complete the following command by running all the settings as default.

We add express.js to our node.js application via npm.

Now let’s create an “index.js” file for the application and add the following codes into it.

Let’s create a file named “cityRoutes.js” and paste the code below into this file.

Let’s add the “start”: “node index.js” field to the “scripts” object to start the application in the “package.json” file and start the application by running the following command.

After the application runs, we get the following output when we send a request to the “http://localhost:3000/api/v1/cities” REST API with Postman. Developing a REST API using Node.js and Express.js is that simple.

Conclusion

We created a simple and fast REST API using Node.js and Express.js and tested the REST API we created through Postman. You can use this REST API that we have created, whether in your mobile applications or desktop applications, regardless of platform.

Exit mobile version