How to Build a RESTful API using Node, Express and MongoDB

Without a doubt, Application Programming Interfaces or APIs play a very important role in communication and data exchange between servers and services. From computers to smartphones and Internet of Things (IoT), applications talk and exchange info via APIs. Almost every day we use or interact with an API service, and many sites use a custom API to scale up their website. As such, learning how to develop a custom API adds lots of value to your developer profile.

Motivated by the importance of APIs and the popularity of JavaScript (JS), we’d like to show you how to develop a custom RESTful API using Node.JS and MongoDB. Along the way, you will be able to leverage and improve your JS skills. Developers can also expand this tutorial by using MySQL instead of MongoDB or securing API by adding authentication call-backs.

About the Project

This tutorial requires an intermediate knowledge of JavaScript and Node.JS along with basic knowledge of, JSON, NPM, MongoDB (a No-SQL database), and Express.JS. This tutorial is geared toward back-end web development as we will not discuss HTML, CSS or Angular. Click here to download the .zip project file for this tutorial. Click here to see its live demo.

Tutorial Overview

In this tutorial, we walk you through seven steps for developing a RESTFul API using popular JavaScript frameworks such as Node.JS and Express.JS. In doing so, we learn how to integrate our API with the MongoDB database. Here are the main steps:

1. Project Initialization
2. Install Application Dependencies
3. Run the Application
4. Test the Application
5. Establish Connection with MongoDB
6. Build REST API Endpoints
7. Put Things Together

Some background on REST APIs is helpful for understanding and following this tutorial.

RESTful API Overview

REST stands for REpresentational State Transfer. When a RESTful API is called, the server will transfer to the client a representation of the state of the requested resource. For example, when a developer calls OpenWeather API to fetch weather for a specific city (the resource), the API will return the state of that city, including the temperature, humidity, wind speed, current forecast, extended forecast, and more. The representation of the state can be in a JSON format, and for most web APIs, this is indeed the case. Other possible data formats include XML or HTML. What does the server does when you call it depends on two things that you need to provide to the server:
1. An identifier for the resource. – This is the URL for the resource, also known as the endpoint. In fact, URL stands for Uniform Resource Locator.
2. The operation you want the server to perform on that resource, in the form of an HTTP method. The common HTTP methods are GET, POST, PUT, and DELETE.

Prerequisites:

  1. Install Node.js by following the appropriate guidelines for your local machine given here.
  2. You may use Node Version Manager to install multiple versions of Node.js on your local machine.
  3. Signup for MongoDB Atlas which is a cloud database as a service. We’re using MongoDB Atlas which offers us a free sandbox environment so that we can focus on building our REST API.
  4. Code Editor like Visual Studio Code or Sublime Text

1. Project Initialization

We need to create an application folder where our application will reside. To initialize the application, we need to create a package.json file, which will hold all the metadata for the node application. The file allows npm to handle installing the package dependencies and scripts we write to handle the application.
Assuming that you’ve Node.js installed, we can initialize the application from the command line by executing the following:

npm init -y

The above command will create a package.json file.

2. Install Application Dependencies

We need a file that can act as command central for our application. This file will be executed first by npm when we ask it to run our application. This file can have object instances of multiple modules that we write as well as third party modules that we install from the npm directory.
These modules are dependencies of the project. We’ll install a few of these dependencies now:

touch app.js
npm install express mongodb body-parser --save

Using the above commands we created a file called app.js which will be the main entry point to the application, and we installed a few dependencies that are essential to start our application.
These dependencies are:
1. Express: This is a Node.js framework.
2. MongoDB: This is an official module provided by the MongoDB team to help our Node.js application communicate with MongoDB.
3. body-parser: This package will allow us to handle request bodies with Express.

Why use body-parser?

Let’s say you are sending an HTML form data to Node.js server i.e. you made a request to the server. HTTP sends your form data in bits and pieces which are intended to get assembled as they reach their destination. To extract these bits and pieces of data and assemble it so it is in a format that can be useful, we use a body-parser middleware.

3. Run the Application

Let’s run the code for our application –

const Express = require("express");
const BodyParser = require("body-parser");
const MongoClient = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectID;
var app = Express();
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
app.listen(5000, () => {});

Here, we’re importing our downloaded dependencies. We initialize the express framework by utilizing the Express object that will use the express framework to start the server and run our application at a given port and configure the body-parser, which is a middleware to parse incoming chunks of data.

4. Test the Application

We can test the application by running the following:

node app.js

The server will be listening on port 5000 for requests according to the above boilerplate we wrote.

5. Establish Connection with MongoDB

We will need the connection string for MongoDB. This can be found in the Atlas dashboard by choosing Clusters, then the Overview tab followed by the Connect button.
We need to add the string to our app.js and make the following changes to the code.

Side note: In an ideal world, developers will benefit from adding it to an .env file to keep their environment variables separate from the application.

const Express = require("express");
const BodyParser = require("body-parser");
const MongoClient = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectID;
const CONNECTION_URL = <YOUR CONNECTION STRING WITH USERNAME AND PASSWORD>;
const DATABASE_NAME = "accounting_department";


var app = Express();
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
var database, collection;

app.listen(5000, () => {
    MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {
        if(error) {
            throw error;
        }
        database = client.db(DATABASE_NAME);
        collection = database.collection("personnel");
        console.log("Connected to `" + DATABASE_NAME + "`!");
    });
});

Here, we’ve defined the connection string to use. You need to add a CONNECTION_URL property according to the connection string you received from MongoDB Atlas. We’ve also defined the database name which we wish to create (if not already present and collection).
We establish a connection once we start again the application and use globally defined variables so that they can be used in each of our endpoints.

Now let’s start designing our HTTP consumable API endpoints.

6. Build REST API Endpoints

Now we’re going to develop endpoints for creating and querying the data. Let’s create an endpoint to add the data. Add the following code to app.js:

app.post("/personnel", (request, response) => {
    collection.insert(request.body, (error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result.result);
    });
});

We aren’t doing any data validation, but when a client makes a POST request to the /personnel endpoint, we take the body and insert it into our collection. Depending on the success or error response of the database, we return information back to the client.

7. Put Things Together

Now, things are starting to come together. Let’s test out our application.

curl -X POST \
    -H 'content-type:application/json' \
    -d '{"firstname":"John","lastname":"Doe"}' \
    http://localhost:5000/personnel

If you do not receive any errors, you’ll see the personnel record for John Doe added into MongoDB database of accounting_department and under collection personnel. Feel free to add more records to your database.

GET

Now let’s create an endpoint to retrieve all the records data. Add the following code to app.js:

 app.get("/personnel", (request, response) => {
    collection.find({}).toArray((error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result);
    });
});

The goal here is to return all data in our collection representing people. We have no query conditions, hence the empty {} in the find command, and the results get converted into an array.
Let’s test this out using cURL, a command-line tool for transferring data and supports HTTP; a very good ad-hoc tool for testing REST services.

curl -X GET http://localhost:5000/personnel

GET (by ID)

By now let’s assume you’ve added more records into your database. API consumers will need to retrieve a single record according to its ID.

Write the following endpoint to your app.js:

app.get("/person/:id", (request, response) => {
    collection.findOne({ "_id": new ObjectId(request.params.id) }, (error, result) => {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result);
    });
});

We’re going to use a route parameter to get specific personnel information from MongoDB. Parameters are options you can pass with the endpoint (such as specifying the response format or the amount returned) to influence the response. This time around we have a route parameter called id which will represent an object id in MongoDB. Using the findOne method, we can get a single record based on the criteria included in the object. The id isn’t a string which is why we have to use the ObjectId.

Depending on the actual data in our database, we can do a cURL command similar to this:

curl -X GET http://localhost:5000/personnel/4b103f89403f841059524fd1

Conclusion

In this tutorial, we’ve learned how to develop a custom API and connect it with MongoDB to fetch and manipulate the data. From here, you can take different steps. For instance, you can expand this project by adding more features like data validation to it. You can also improve the API security and salability by following Node.JS best practices. Lastly, you can try this tutorial with MySQL database or another server-side coding language like PHP or Django.

What do you think of this tutorial? What tools are you using to develop REST APIs?

#node-js #express #mongodb #web-development #api

What is GEEK

Buddha Community

How to Build a RESTful API using Node, Express and MongoDB
Wilford  Pagac

Wilford Pagac

1594289280

What is REST API? An Overview | Liquid Web

What is REST?

The REST acronym is defined as a “REpresentational State Transfer” and is designed to take advantage of existing HTTP protocols when used for Web APIs. It is very flexible in that it is not tied to resources or methods and has the ability to handle different calls and data formats. Because REST API is not constrained to an XML format like SOAP, it can return multiple other formats depending on what is needed. If a service adheres to this style, it is considered a “RESTful” application. REST allows components to access and manage functions within another application.

REST was initially defined in a dissertation by Roy Fielding’s twenty years ago. He proposed these standards as an alternative to SOAP (The Simple Object Access Protocol is a simple standard for accessing objects and exchanging structured messages within a distributed computing environment). REST (or RESTful) defines the general rules used to regulate the interactions between web apps utilizing the HTTP protocol for CRUD (create, retrieve, update, delete) operations.

What is an API?

An API (or Application Programming Interface) provides a method of interaction between two systems.

What is a RESTful API?

A RESTful API (or application program interface) uses HTTP requests to GET, PUT, POST, and DELETE data following the REST standards. This allows two pieces of software to communicate with each other. In essence, REST API is a set of remote calls using standard methods to return data in a specific format.

The systems that interact in this manner can be very different. Each app may use a unique programming language, operating system, database, etc. So, how do we create a system that can easily communicate and understand other apps?? This is where the Rest API is used as an interaction system.

When using a RESTful API, we should determine in advance what resources we want to expose to the outside world. Typically, the RESTful API service is implemented, keeping the following ideas in mind:

  • Format: There should be no restrictions on the data exchange format
  • Implementation: REST is based entirely on HTTP
  • Service Definition: Because REST is very flexible, API can be modified to ensure the application understands the request/response format.
  • The RESTful API focuses on resources and how efficiently you perform operations with it using HTTP.

The features of the REST API design style state:

  • Each entity must have a unique identifier.
  • Standard methods should be used to read and modify data.
  • It should provide support for different types of resources.
  • The interactions should be stateless.

For REST to fit this model, we must adhere to the following rules:

  • Client-Server Architecture: The interface is separate from the server-side data repository. This affords flexibility and the development of components independently of each other.
  • Detachment: The client connections are not stored on the server between requests.
  • Cacheability: It must be explicitly stated whether the client can store responses.
  • Multi-level: The API should work whether it interacts directly with a server or through an additional layer, like a load balancer.

#tutorials #api #application #application programming interface #crud #http #json #programming #protocols #representational state transfer #rest #rest api #rest api graphql #rest api json #rest api xml #restful #soap #xml #yaml

An API-First Approach For Designing Restful APIs | Hacker Noon

I’ve been working with Restful APIs for some time now and one thing that I love to do is to talk about APIs.

So, today I will show you how to build an API using the API-First approach and Design First with OpenAPI Specification.

First thing first, if you don’t know what’s an API-First approach means, it would be nice you stop reading this and check the blog post that I wrote to the Farfetchs blog where I explain everything that you need to know to start an API using API-First.

Preparing the ground

Before you get your hands dirty, let’s prepare the ground and understand the use case that will be developed.

Tools

If you desire to reproduce the examples that will be shown here, you will need some of those items below.

  • NodeJS
  • OpenAPI Specification
  • Text Editor (I’ll use VSCode)
  • Command Line

Use Case

To keep easy to understand, let’s use the Todo List App, it is a very common concept beyond the software development community.

#api #rest-api #openai #api-first-development #api-design #apis #restful-apis #restful-api

Franz  Bosco

Franz Bosco

1624268558

Build RESTful API Using Express, MongoDB & NodeJS | Q & A

As promised during the session, you can avail the Webinar offer

Offer 1:
Buy the Live Online MERN Stack E-Degree At Flat $120!
& Get JavaScript Mini E-Degree absolutely FREE
Offer link: https://bit.ly/3gBQxgV
Offer Valid for next 48 Hours only!

Offer 2:
3 Lucky Winners will get Eduonix Vouchers Worth $50! Use the Voucher to buy any Course from the Eduonix Website!

APIs or Application Programming Interfaces are almost everywhere now. However, REST (Representational State Transfer) brings out the true potential of APIs.
REST or Representational State Transfer is nothing but a set of rules that developers follow while creating the respective APIs. Soon after its introduction, RESTful APIs become the most popular ones and can be used for almost any protocol. Thanks to its flexibility and a myriad of other advantages, big giants like Amazon, Twitter, Facebook, eBay, Flickr & others started implementing it.

This Live Session will cover topics like:

  • Express, Mongoose Installation
  • Connecting to Database
  • Creating Schemas and Models
  • Executing Queries
  • Rest API using Express
    For any queries related to the session, please email us at info@eduonix.com

#node #mongodb #express #api #restful

Troy  Marvin

Troy Marvin

1605594300

REST API with Express.js | Node | MongoDB - Node JS Tutorial

Learn how to create a REST API using Express.js, Node and MongoDB. Understand how to use Express.js and a look into what middleware is, and to implement your own middleware. How to manage errors to be consistent and scalable. And connect to your MongoDB database to store information.

MongoDB Atlas: https://www.mongodb.com/cloud/atlas

#rest #api #node #express #mongodb

Riaan  Nkuna

Riaan Nkuna

1605714780

REST API with Express.js | Node | MongoDB - Node JS Tutorial

Learn how to create a REST API using Express.js, Node and MongoDB. Understand how to use Express.js and a look into what middleware is, and to implement your own middleware. How to manage errors to be consistent and scalable. And connect to your MongoDB database to store information.

MongoDB Atlas: https://www.mongodb.com/cloud/atlas

#rest #api #node #express #mongodb