Create your own REST API using node.js and Mongo (Part 1 ) Setting it all up

In this post we are going to take a look at how to create a REST API using Node.js and Mongo to be used with your web or mobile application. But before we jump in let me first explain what exactly a REST API is.

REST API is a way of communication between the client and the server. REST APIs uses HTTP methodologies (GET, PUT, DELETE, POST) to manipulate resources. GET is used to retrieve resources from the server, PUT is used to change or update resources, POST adds new resources and DELETE does exactly what it says, it deletes resources.

MongoDB stores data using a  document data model  similar to JSON. These documents can contain one or more fields, arrays or sub-documents. Each field can vary from one document to another allowing  great flexibility of the data model.

A practical example of where a REST API will be used is to establish communication between a mobile application (client) and a database (server). The user wants to see all the latest available movies showing at the cinema using the mobile application (client). The mobile app sends a GET request to the REST API. The REST API complete this request by retrieving the requested data (typically from a database etc) and responds with the latest movies showing at the cinema. The mobile app gets the list of movies back from the REST API and displays it on the screen to the user.

OK so now that I explained what a REST API is and a typical scenario of where it could be used let’s set up the environment to create our own REST API using node.js and MongoDB.

Create a project folder

Create a folder for the project and set it up for node.js by opening the command prompt window inside the folder you have just created and use the following command:

$ npm init

This will create a package.json file for you inside  the project folder. Package.json files typically contains some meta data about your app and includes the list of dependancies to install from npm when the npm install command is used.

Install MongoDB

Go to the mongodb website: http://mongodb.org to download Mongo on your pc. You can use the documentation available on the website to install Mongo

Once Mongo is installed you will need to run it by opening the command prompt and navigating to the installation directory where Mongo is installed. In the command prompt type

$ mongod

and “Enter”. This will run Mongo Deamon, which is a background process that handles the data requests. You should now see in your command prompt that Mongo is running or waiting for connections.

Install Node.js

Node is available from the website https://nodejs.org  Download it and follow the installation instructions (Use the LTS version).

Create a JS file

Inside the project folder create a js file. In this scenario I will create a file called “greetings.js”

Inside the file type the following:

console.log(“Hello World!”);

Execute the js file by typing into the command prompt:

$ node greetings.js

You should now be able to see “Hello World!” displayed in the command window.

Final Thoughts

Well that is it for Part 1. Be sure to check out Part 2 that will contain the guide to creating a database in Mongo.

 

Advertisements

One thought on “Create your own REST API using node.js and Mongo (Part 1 ) Setting it all up

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s