googleads
Restful API using MEAN stack | Hire MEAN Stack Development Services
preloder
API Standard Technical Web Development

Restful API using MEAN stack

This article explores the basics of the MEAN stack, shows how to create a DB Connection and also Create APIs (GET, POST, DELETE).

Prerequisites to be in your machine:

* Node
* MongoDB

Installations to be done:

* npm install express –save
Before going into the application setup, we need to know what is MEAN stack.

What is MEAN stack?

The term MEAN stack refers to a collection of JavaScript based technologies used to develop web applications. MEAN is an acronym for MongoDB, ExpressJS, AngularJS and Node.js. From client to server to database, MEAN is full stack JavaScript. This article describes the basics of the MEAN stack and shows how to create a simple bucket list application.

Other basic definitions that are to be known:

* Node.js is a server side JavaScript execution environment. It’s a platform built on Google Chrome’s V8 JavaScript runtime. It helps in building highly scalable and concurrent applications rapidly.
* Express is lightweight framework used to build web applications in Node. It provides a number of robust features for building single and multi page web application. Express is inspired by the popular Ruby framework, Sinatra.
* MongoDB is a schemaless NoSQL database system. MongoDB saves data in binary JSON format which makes it easier to pass data between client and server.
* AngularJS is a JavaScript framework developed by Google. It provides some awesome features like the two-way data binding. It’s a complete solution for rapid and awesome front end development.

Create the Application Structure:

We are going to create an application called MyBooks where you can create, edit and delete your own books. Let’s create the application structure using the application generator tool, “express-generator”, to quickly create an application skeleton.
For this purpose, give the following command:
* $express mybooks

DB Connection:

Create a folder called db and create the file called connector.js which will provide the DB connections.
mybooks/db/connector.js is the file that is going to serve the mongodb connection for our mybooks application by the help of MongoClient object. To create an object so as to interact with the MongoDB:

 

The state variable holds the db connection.

 

Connect the db through the MongoDB connection string in url format

 

Return the connection object which is used in the DB operations

 

Finally, we close the DB connection:

 

In our example, by default, the server setup exists in the mybooks/bin/www. Here, we will establish the db connection.
We are going to create the db connection at the beginning of the application itself, and it will hold good till the end of it, without having to establish connections for each db operation. To do this:
Find the Following lines in www
server.listen(port);
server.on(‘error’, onError);
server.on(‘listening’, onListening);
replace this lines with the following lines

 

If at all there is some error in the db connection, the application itself will not get started.
lets create a file called api.js inside the router folder. In our case, it shall be mybooks/routes/api.js.

Performing Operations on the DB with the connection that we have created:

1. To create the records in collection:

 

2. To find the record with the collection id

 

3. Update the document in collection by id

 

4. And finally, Delete the document by id:

 

Last but not least, we shall include the common error handling function that shall deal with all connection errors.

 

Conclusion:

Now, we have to run the application in order for the setup to take effect. For this, we provide the command:
npm start
This will run the application, the default being http://localhost:3000.
Using a third party API test tool like postman, we can access our APIs through the appropriate URLs.
In this article we have learnt the basics of Mean stack and how to connect the db and perform CRUD operations through the API.

The following two tabs change content below.

Arunkumar Nattarayan

Latest posts by Arunkumar Nattarayan (see all)