Sunday, February 11, 2018

Mongoose API

In this tutorial, We will be building a simple Note-Taking application. We will build Rest APIs for creating, listing, editing and deleting a Note.
We’ll start by building a simple web server and then move on to configuring the database, building the Note model and different routes for handling all the CRUD operations.
Finally, we’ll test our REST APIs using Postman.
Well! Now that we know what we are going to build, We need a cool name for our application. Let’s call our application EasyNotes.

Creating the Application

1. Fire up your terminal and create a new folder for the application.
$ mkdir node-easy-notes-app
2. Initialize the application with a package.json file
Go to the root folder of your application and type npm init to initialize your app with a package.json file.
$ cd node-easy-notes-app
$ npm init
name: (node-easy-notes-app)
version: (1.0.0)
description: Never miss a thing in Life. Take notes quickly. Organize and keep track of all your notes.
entry point: (index.js) server.js
test command:
git repository:
keywords: Express RestAPI MongoDB Mongoose Notes
author: callicoder
license: (ISC) MIT
About to write to /Users/rajeevkumarsingh/node-easy-notes-app/package.json:

{
 "name": "node-easy-notes-app",
 "version": "1.0.0",
 "description": "Never miss a thing in Life. Take notes quickly. Organize and keep track of all your notes.",
 "main": "server.js",
 "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [
   "Express",
   "RestAPI",
   "MongoDB",
   "Mongoose",
   "Notes"
 ],
 "author": "callicoder",
 "license": "MIT"
}

Is this ok? (yes) yes
Note that I’ve specified a file named server.js as the entry point of our application. We’ll create server.js file in the next section.
3. Install dependencies
We will need express, mongoose and body-parser modules in our application. Let’s install them by typing the following command -
$ npm install express body-parser mongoose --save
I’ve used --save option to save all the dependencies in the package.json file. The final package.json file looks like this -

{
 "name": "node-easy-notes-app",
 "version": "1.0.0",
 "description": "Never miss a thing in Life. Take notes quickly. Organize and keep track of all your notes.",
 "main": "server.js",
 "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [
   "Express",
   "RestAPI",
   "MongoDB",
   "Mongoose",
   "Notes"
 ],
 "author": "callicoder",
 "license": "MIT",
 "dependencies": {
   "body-parser": "^1.18.2",
   "express": "^4.16.2",
   "mongoose": "^4.13.6"
 }
}
Our application folder now has a package.json file and a node_modules folder -
node-easy-notes-app
   └── node_modules/
   └── package.json

Setting up the web server

Let’s now create the main entry point of our application. Create a new file named server.js in the root folder of the application with the following contents -

var express = require('express');
var bodyParser = require('body-parser');

// create express app
var app = express();

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))

// parse requests of content-type - application/json
app.use(bodyParser.json())

// define a simple route
app.get('/', function(req, res){
   res.json({"message": "Welcome to EasyNotes application. Take notes quickly. Organize and keep track of all your notes."});
});

// listen for requests
app.listen(3000, function(){
   console.log("Server is listening on port 3000");
});
First, We import express and body-parser modules. Express, as you know, is a web framework that we’ll be using for building the REST APIs, and body-parser is a module that parses the request (of various content types) and creates a req.body object that we can access in our routes.
Then, We create an express app, and add two body-parser middlewares using express’s app.use() method. A middleware is a function that has access to the request and response objects. It can execute any code, transform the request object, or return a response.
Then, We define a simple GET route which returns a welcome message to the clients.
Finally, We listen on port 3000 for incoming connections.
All right! Let’s now run the server and go to http://localhost:3000 to access the route we just defined.
$ node server.js
Server is listening on port 3000

Configuring and Connecting to the database

I like to keep all the configurations for the app in a separate folder. Let’s create a new folder config in the root folder of our application for keeping all the configurations -
$ mkdir config
$ cd config
Now, Create a new file database.config.js inside config folder with the following contents -
module.exports = {
   url: 'mongodb://localhost:27017/easy-notes'
}
We’ll now import the above database configuration in server.js and connect to the database using mongoose.
Add the following code to the server.js file after app.use(bodyParser.json()) line -
// Configuring the database
var dbConfig = require('./config/database.config.js');
var mongoose = require('mongoose');

mongoose.connect(dbConfig.url, {
   useMongoClient: true
});

mongoose.connection.on('error', function() {
   console.log('Could not connect to the database. Exiting now...');
   process.exit();
});

mongoose.connection.once('open', function() {
   console.log("Successfully connected to the database");
})
Please run the server and make sure that you’re able to connect to the database -
$ node server.js
Server is listening on port 3000
Successfully connected to the database

Defining the Note model in Mongoose

Next, We will define the Note model. Create a new folder called app inside the root folder of the application, then create another folder called models inside the app folder -
$ mkdir -p app/models
$ cd app/models
Now, create a file called note.model.js inside app/models folder with the following contents -

var mongoose = require('mongoose');

var NoteSchema = mongoose.Schema({
   title: String,
   content: String
}, {
   timestamps: true
});

module.exports = mongoose.model('Note', NoteSchema);
The Note model is very simple. It contains a title and a content field. I have also added a timestamps option to the schema.
Mongoose uses this option to automatically add two new fields - createdAt and updatedAt to the schema.

Defining Routes using Express

Next up is the routes for the Notes APIs. Create a new folder called routes inside the app folder.
$ mkdir app/routes
$ cd app/routes
Now, create a new file called note.routes.js inside app/routes folder with the following contents -

module.exports = function(app) {

   var notes = require('../controllers/note.controller.js');

   // Create a new Note
   app.post('/notes', notes.create);

   // Retrieve all Notes
   app.get('/notes', notes.findAll);

   // Retrieve a single Note with noteId
   app.get('/notes/:noteId', notes.findOne);

   // Update a Note with noteId
   app.put('/notes/:noteId', notes.update);

   // Delete a Note with noteId
   app.delete('/notes/:noteId', notes.delete);
}
Note that We have added a require statement for note.controller.js file. We’ll define the controller file in the next section. The controller will contain methods for handling all the CRUD operations.
Before defining the controller, let’s first include the routes in server.js. Add the following require statement before app.listen() line inside server.js file.
// ........

// Require Notes routes
require('./app/routes/note.routes.js')(app);

// ........
If you run the server now, you’ll get the following error -
$ node server.js
module.js:472
   throw err;
   ^

Error: Cannot find module '../controllers/note.controller.js'
This is because we haven’t defined the controller yet. Let’s do that now.

Writing the Controller functions

Create a new folder called controllers inside the app folder, then create a new file called note.controller.js inside app/controllers folder with the following contents -

var Note = require('../models/note.model.js');

exports.create = function(req, res) {
   // Create and Save a new Note

};

exports.findAll = function(req, res) {
   // Retrieve and return all notes from the database.

};

exports.findOne = function(req, res) {
   // Find a single note with a noteId

};

exports.update = function(req, res) {
   // Update a note identified by the noteId in the request

};

exports.delete = function(req, res) {
   // Delete a note with the specified noteId in the request

};
Let’s now look at the implementation of the above controller functions one by one -

Creating a new Note

exports.create = function(req, res) {
   // Create and Save a new Note
   if(!req.body.content) {
       res.status(400).send({message: "Note can not be empty"});
   }
    var note = new Note({title: req.body.title || "Untitled Note", content: req.body.content});

   note.save(function(err, data) {
       console.log(data);
       if(err) {
           console.log(err);
           res.status(500).send({message: "Some error occurred while creating the Note."});
       } else {
           res.send(data);
       }
   });
};

Retrieving all Notes

exports.findAll = function(req, res) {
   // Retrieve and return all notes from the database.
   Note.find(function(err, notes){
       if(err) {
           res.status(500).send({message: "Some error occurred while retrieving notes."});
       } else {
           res.send(notes);
       }
   });
};

Retrieving a single Note

exports.findOne = function(req, res) {
   // Find a single note with a noteId
   Note.findById(req.params.noteId, function(err, data) {
       if(err) {
           res.status(500).send({message: "Could not retrieve note with id " + req.params.noteId});
       } else {
           res.send(data);
       }
   });
};

Updating a Note

exports.update = function(req, res) {
   // Update a note identified by the noteId in the request
   Note.findById(req.params.noteId, function(err, note) {
       if(err) {
           res.status(500).send({message: "Could not find a note with id " + req.params.noteId});
       }

       note.title = req.body.title;
       note.content = req.body.content;

       note.save(function(err, data){
           if(err) {
               res.status(500).send({message: "Could not update note with id " + req.params.noteId});
           } else {
               res.send(data);
           }
       });
   });
};

Deleting a Note

exports.delete = function(req, res) {
   // Delete a note with the specified noteId in the request
   Note.remove({_id: req.params.noteId}, function(err, data) {
       if(err) {
           res.status(500).send({message: "Could not delete note with id " + req.params.id});
       } else {
           res.send({message: "Note deleted successfully!"})
       }
   });
};

Testing our APIs

Creating a new Note using POST /notes API

Node Express Rest API Create a Note

Retrieving all Notes using GET /notes API

Node Express Rest API Retrieve All Notes

Retrieving a single Note using GET /notes/:noteId API

Node Express Rest API Retrieve a Single Note

Updating a Note using PUT /notes/:noteId API

Node Express Rest API Update a Note

Deleting a Note using DELETE /notes/:noteId API

Node Express Rest API Delete a Note

API - Application Programming Interface

To build an application program it is a set of clearly defined methods of communication between various software components. A good API makes it easier to develop a computer program by providing all the building blocks, which are then put together by the programmer.

An API may be for a web-based system, operating system, database system, computer hardware or software library. An API specification can take many forms, but often includes specifications for routines, data structures, object classes, variables or remote calls.

POSIX, Windows API and ASPI are examples of different forms of APIs. Documentation for the API is usually provided to facilitate usage.

When you use an application on your mobile phone, the application connects to the Internet and sends data to a server. The server then retrieves that data, interprets it, performs the necessary actions and sends it back to your phone. The application then interprets that data and presents you with the information you wanted in a readable way.

This is what an API is - all of this happens via API.

Wednesday, February 7, 2018

Mongoose

Mongoose is an object modeling package for Node that essentially works like an ORM (Object-relational mapping ) that you would see in other languages.
Mongoose allows us to have access to the MongoDB commands for CRUD simply and easily.

Mongoose Installation

npm install mongoose --save

Now that we have the package, we just
have to grab it in our project

var mongoose = require('mongoose');

We also have to connect to a MongoDB database (either local or hosted)

mongoose.connect('mongodb://localhost/myappdatabase');

Terminology of Mongoose

Mongoose will use the terms Schema
and Model.

What is Schema?

"Mongoose Schema will create a mongodb collection and defines the shape of the documents within that collection".

If we want to create a mongodb collection in a structured manner similar to SQL tables then we can do that using mongoose Schema.

In the schema creation we will specify the details of fields like field name, its type, default value if need, index concept if need, constraint concept if need.

What is Model?

Schemas define a structure we will apply that Schema part to a model, means "Models are the constructors compiled from our schema
definitions".

Instances of these models represent documents which can be saved and retrieved from our database. All document creation and retrieval from the database is handled by these models

Key Methods and Properties of Mongoose

Connect() :- Opens the default mongoose
connection.

CreateConnection()  :- Will creates a connection
instance and its work is similar to connect()

Schema() :- The Mongoose Schema constructor

Model() :- The Mongoose Model constructor

Document() :- The Mongoose Document constructor.

Disconnect() :-  Disconnects all connections.

Mongoose Connection

Open(): Opens the connection to MongoDB.

OpenSet(): Opens the connection to a replica
set.

close(): Closes the connection, it doesn’t throw
any error even the connection is not opened or
already closed.

readyState: It is property of connection which holds state of the connection

Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It
facilitates the rapid development of Node based Web
applications.

Features of Express framework
  • Allows to set up middlewares to respond to HTTP Requests.
  • Defines a routing table which is used to perform different
  • actions based on HTTP Method and URL.
  • Allows to dynamically render HTML Pages based on passing arguments to templates

Installing Express
$ npm install express --save

You should install the following important modules along with express −

body-parser
− This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.

cookie-parser
− Parse Cookie header and populate req.cookies with an object keyed by the cookie names.

multer
− This is a node.js middleware for handling multipart/form-data.

$ npm install body-parser –save
$ npm install cookie-parser –save
$ npm install multer --save

AJAX

AJAX = Asynchronous JavaScript And XML.

AJAX just uses a combination of:

A browser built-in XMLHttpRequest object (to request
data from a web server)
JavaScript and HTML DOM (to display or use the
data)

AJAX allows web pages to be updated asynchronously by exchanging data with a
web server behind the scenes. This means that it is possible to update parts of a webpage, without reloading the whole page.

1. An event occurs in a web page (the page is loaded, a button is clicked)
2. An XMLHttpRequest object is created by  JavaScript
3. The XMLHttpRequest object sends a  request to a web server
4. The server processes the request
5. The server sends a response back to the web page
6. The response is read by JavaScript
7. Proper action (like page update) is performed by JavaScript








AJAX is not a programming language.

What AJAX Do..??
  • Update a web page without reloading the
  • page
  • Request data from a server - after the page
  • has loaded
  • Receive data from a server - after the page
  • has loaded
  • Send data to a server - in the background