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.
Table of Contents
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.
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
- It provides the opportunity to develop Web applications easily and quickly.
- Template engines such as Pug2, EJS3, Handlebars can be used in application development processes.
- Comprehensive applications can be developed with middleware modules,
- Web requests can be managed with the Route method.
- Database applications such as MySQL, MongoDB, SQL SERVER can be used in applications.
- It is possible to create a REST API for different applications.
- Static files are easy to manage.
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.
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.