Table of contents
1.
Introduction
2.
What is an API?
3.
What are Node and Express? 
4.
Steps to create API using Node and Express
4.1.
Step 1 : Install NodeJS and NPM
4.2.
Step 2: Setting up our NodeJs and Express project
4.3.
Step 3: In a code editor, open the package.json file.
4.4.
Step 4: Creating a simple GET route
4.5.
Step 5: Let's start the Express web application by running the following command
4.6.
Step 6: Now, in a browser, go to http://localhost:3000
5.
Frequently Asked Questions
5.1.
What is an API?
5.2.
Why do we need APIs?
5.3.
What is an API endpoint?
5.4.
What are some real-world APIs?
5.5.
What is an API gateway?
6.
Conclusions 
Last Updated: Mar 27, 2024

Creating a simple API with nodeJS and Express

Author Jay
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas! Are you looking to create your API but need help with where to start? Not to worry, because today, we're diving into the exciting world of building APIs in NodeJs and Express.

Creating a simple API in NodeJS and ExpressJS

Firstly, don't let the fear of not being advanced enough hold you back. NodeJs and Express are some of the most beginner-friendly tools out there, making creating APIs more manageable than it may seem.

What is an API?

An API (Application Programming Interface) is a set of rules and protocols allowing different software applications to communicate. Think of it like an intermediary who passes a message between two parties.

An API can be considered a middleman between users or customers and the resources or web services they want. It's also a mechanism for a company to exchange resources and information while retaining security, control, and authentication—the ability to govern who has access to what.

So, why create your API? The reasons are endless! You may be interested in building a mobile app that connects to a database or creating a chatbot that interacts with users.

In this beginner-friendly blog, we'll take you through the entire process of creating your API in NodeJs and Express. We'll cover everything from setting up your development environment to handling requests and creating routes.

What are Node and Express? 

Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside a web browser. With Node.js, you can use JavaScript to build server-side applications and APIs. This means that you can write your entire web application using JavaScript, both on the client side (in the browser) and on the server side.

Express.js, on the other hand, is a web framework for Node.js. It provides tools and features that make building web applications and APIs easier. With Express.js, you can create routes, handle HTTP requests and responses, serve static files, and more.

Think of Node.js as the engine that powers your server-side JavaScript code and Express.js as the toolbox that helps you build your web application. Together, Node.js and Express.js make it easy to write robust and scalable web applications using JavaScript.

Steps to create API using Node and Express

Let us dive into the world of creating APIs in NodeJs and Express!

Step 1 : Install NodeJS and NPM

We have already discussed it on NodeJS.

NPM, or Node Package Manager, is a tool that allows you to manage the packages and dependencies that your NodeJS application will use.

To install NodeJS and NPM on your machine, follow these steps:

  • Go to the official NodeJS website at https://nodejs.org.
  • Click the "Download" button to download the installer for your operating system.
  • Run the installer and follow the prompts to install NodeJS and NPM on your machine.
  • After installation, open a terminal or command prompt and type the following commands to verify that NodeJS and NPM have been installed correctly:
     
node -v
npm -v
check version

These commands will print the version numbers of NodeJS and NPM, respectively as shown in the above picture.

Step 2: Setting up our NodeJs and Express project

First things first, let's set up our NodeJs and Express project. We'll need to create a new folder for our project, open up the command prompt or terminal, navigate to our project folder, and run the npm init command to initialise our project. Once we've initialized our project, we'll install the Express framework and create a new file called index.js to write our code in.

Practice npm install to frame a new project in the folder you just created , my-first-api in my case.

npm install
npm init -y
npm install express
You can also try this code with Online Javascript Compiler
Run Code
npm init

The init command creates a default package.json file for your Node.js project. 

install express

The install command installs the Express framework.

Step 3: In a code editor, open the package.json file.

In the dependencies section, locate the express entry:

"dependencies": {
  "express": "^4.18.1"
You can also try this code with Online Javascript Compiler
Run Code

This entry indicates the Express framework is installed. Now close the file.

Step 4: Creating a simple GET route

Now that our project is set up let's create our first API route. We'll start with a simple GET route that listens for requests to the URL / and responds with a message. To do this, we'll add the following code to our index.js file:

const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World , This is my first API !');
});
app.listen(3000, () => {
  console.log('Server is listening on port 3000');
});
You can also try this code with Online Javascript Compiler
Run Code

Firstly , let's understand what each of these lines means.

const express = require('express');

This line imports the ExpressJS module so that you can use it in your code.

const app = express();

This line creates a new application using the ExpressJS module that you imported.

app.get('/', (req, res) => {
 res.send('Hello World , This is my first API !');
});

This line defines a route for your API at the root path ('/'). When a client makes a GET request to this route, the server will send back the message "Hello World, This is my first API!".

app.listen(3000, () => {
  console.log('Server is listening on port 3000');
});
You can also try this code with Online Javascript Compiler
Run Code

This line starts the server and tells it to listen for incoming requests on port 3000. When the server starts listening, it will print the message "Server is listening on port 3000" to the console.

api

This is how your work space and code structure will look like.

Step 5: Let's start the Express web application by running the following command

node index.js
You can also try this code with Online Javascript Compiler
Run Code
node index.js

Step 6: Now, in a browser, go to http://localhost:3000

example

You will get a response as shown in the picture above.

You can even send GET requests using tools like postman.

postman

Frequently Asked Questions

What is an API?

An API, or Application Programming Interface, is a set of protocols and tools for building software applications. APIs define how software components should interact and communicate with each other, allowing different applications to communicate and share data.

Why do we need APIs?

APIs allow developers to build applications that can communicate with other applications, services, or data sources. This can streamline the development process, increase efficiency, and allow for more complex and powerful applications.

What is an API endpoint?

An API endpoint is the URL where you can access a particular API. It is the location where the API service can be accessed by a client application.

What are some real-world APIs?

Some real-world APIs are Facebook API, WhatsApp API, Twitter API, Google Maps API.

What is an API gateway?

An API gateway is a server that acts as an intermediary between an application and an API. It can perform tasks such as rate limiting, authentication, and routing requests to the appropriate service.

Conclusions 

Through this blog , you  became familiar with the APIs . You should be feel fairly confident about how to create an API by using Express and Node.js. As part of learning to construct APIs, you looked at different ways of interacting with your API. You found that you could use a browser or agent like  postman to send requests. Now you  have a good foundation to build web applications with Express. You also understand that there's more to learn about how to create and work with APIs, especially in securing your API.

We hope this blog has helped you in creating your first API using Node and Express.If you want to learn more, then check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

 

 

Live masterclass