Hit enter after type your search item

Node.js Express – How to Write a Simple RESTful API in Node.js

/
/
/
95 Views

In this post, we are going to learn how to write a simple Node.js Express application. We will store the data in a file and do the CRUD operations instead of using a MongoDB.

The idea behind not using the MongoDB is to get a clear picture of how the data storage works, so this example Node.js application will definitely help you to understand the basics.

Express helps to create a web application easily.

Let’s get into the process instead of talking.

If you don’t have it, please download and install it.

Step 1: Initialize a node project

After creating a folder in your system, give any name you want, and then open the command prompt and run the below command inside it.

$ npm -y init

If you run the above command, you will see a file with few lines of code.

“name”, “version”, “description”, “main”, “app.js”, “scripts”, and “test”

Don’t worry, just make sure the file has been created. We need to install the framework.

Step 2: Install the required packages

To install the express nodejs tutorial package, we need to run the below command.

$ npm i [email protected]

The package will be installed on your system.

Step 3: Let’s start writing CRUD operations

If everything is up and running, open the app.js file and use the below lines of code.

The const output is the value of ‘hello world!’

Run the below command to start the server after saving the above file.

$ node app.js

If you open any browser and type “http://localhost:3000”, you will see the output.

If it works, we should create another file users.json and write an empty square brackets.

I’m going to put the entire app.js code here with the inline comments so you can easily understand it.

In Node.js there is a CRUD.

This line is required to create a POST method.

If you are already good at Javascript, you don’t need to go through the inline comments to understand the logic.

If the server is already running the below command, you can save the file and run it again.

$ node app.js

We need to test all of the methods in app.js to make sure we have the right one. You can test Postman if you know what it is. I will show you how to do it with a game.

Step 4: Test our application

  • CREATE Method (http://localhost:3000/user/add)

Click the “Send” button if you want to see the output after you fill the “Request body” shown in the picture below.

Everything is working fine if you see the same thing as the above picture.

The user data should be written in the users.json file when you open it.

The Read method can be tested in this way.

  • READ Method (http://localhost:3000/user/list)

You can open Postman chrome extension and use the method as shown in the below example.

The response output is shown in the above picture.

  • Update Method (http://localhost:3000/user/update/:username) – :username is dynamic

You can open Postman chrome extension and put the URL as shown in the below example. Put the request body as well.

Let’s check the deleted method as well.

  • DELETE Method (http://localhost:3000/user/delete/:username) – :username is dynamic

Go back to Postman chrome extension and select the method that you want to uninstall. The request body is not required for the operation.

I hope the post helped you understand the operation. Happy learning

Share this: Twitter

  • Facebook

Leave a Comment

Your email address will not be published.

This div height required for enabling the sticky sidebar