Quick Contact


    MEAN Stack – Building an SPA

    We created a single-page Meanjs application using Angularjs in the previous chapter. Let’s look at how an Angular application uses API to obtain data from Mongodb in this chapter.

    This link will take you to the source code for this programme. Download the zip file and unzip it on your computer.

    What is SPA?

    The web application changes the content of the main page for future requests in a Single-Page Application (SPA) design. Markup and data are requested separately under this architecture, and pages are rendered in the browser.

    Instead of sending and getting a whole html page as a request and response, SPA sends and receives json data from the server. This lowers the size of the request and answer, making it quicker for a better customer experience.

    Advantages of SPA:
    • Fast, because most resources are only loaded once throughout the application’s lifetime.
    • Streamlined and simplified development
    • There is a shorter learning curve.
    • Chrome makes debugging a breeze.
    • Making a mobile application is easier since the backend may be reused.

    Let’s first understand what is MEAN Stack?

    MEAN stack is a full-stack JavaScript development trend that is still evolving. MEAN is an acronym that stands for:

    MongoDB:

    There is no SQL database.

    For data storage, JSON documents are used.

    ExpressJS:

    Framework for HTTP servers

    Provides functionality for your REST API, such as cookies, routes, and folder structure.

    Angular:

    In today’s industry, the most frequently used client-side framework is

    TypeScript was used to create the project, and TypeScript was also used to code it.

    For mobile development, it’s efficient.

    Node.js:

    JavaScript framework for developing scalable and quick web apps and APIs on the server side.

    It’s small and light, making it ideal for real-time applications.

    MEAN Stack is preferred for the following reasons:

    All of the technologies used are free and open source.

    Technology Stack:

    Various technologies may be used to create SPA apps. The following are the major areas in which we need to improve in SPA:

    Data is stored in a database.

    To communicate with a database, use a server-side REST API.

    To create a client-side application, you’ll need a client-side framework.

    To construct this, we may use whatever technology stack we choose. The following are some examples:

    SQL Server, Oracle, MySQL, and Mongo DB are examples of databases.

    .NET Web API, Code Igniter, Java, Node JS, and Express REST API

    REACT JS / AngularJS client-side application

    Why are we choosing MEAN stack above all of these technologies if we have a choice?

    The following is the directory structure of our source code:

    mean-sample tutorials.ducatindia.com

    		   -app
    	      -models
    	         -student.js
    	   -config
    	      -db.js
    	   -public
    	      -js
    	         -controllers
    	            -MainCtrl.js
    	            -StudentCtrl.js
    	         -services
    	            -StudentService.js
    	         -app.js
    	         -appRoutes.js
    	      -views
    	         -home.html
    	         -student.html
    	      -index.html
    	   -.bowerrc
    	   -bower.json
    	   -package.json
    	   -server.js
    	

    We have developed a view (home.html) in this application that will show all students from the Student collection, allow us to create a new student record, and allow us to remove the student record. REST API calls are used to carry out all of these tasks.

    To install npm module dependencies, open the terminal and type the following command.

    $ npm install

    The server.js file will save the node settings for an application. This is the node app’s primary file, which will configure the whole programme.

    Code:-

    	const express = require('express');
    	const app = express();
    	varbodyParser = require('body-parser');
    	var mongoose = require('mongoose');
    	varmethodOverride = require('method-override');
    	// set our port
    	const port = 3000;
    	// configuration 
    	// configure body parser
    	app.use(bodyParser.json()); // parse application/json
    
    	// parse application/vnd.api+json as json
    	app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
    
    	// parse application/x-www-form-urlencoded
    	app.use(bodyParser.urlencoded({ extended: true }));
    
    	// override with the X-HTTP-Method-Override header in the request.
    	app.use(methodOverride('X-HTTP-Method-Override')); simulate DELETE/PUT
    
    	// set the static files location /public/img will be /img for users
    	app.use(express.static(__dirname + '/public'));
    
    	// config files
    	vardb = require('./config/db');
    	console.log("connecting--",db);
    	mongoose.connect(db.url); //Mongoose connection created
    
    	// grab the student model
    	var Student = require('./app/models/student');
    	functiongetStudents(res) {
    	Student.find(function (err, students) {
    	      // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    	if (err) {
    	res.send(err);
    	         }
    	res.json(students); // return all todos in JSON format
    	      });
    	   };
    	app.get('/api/studentslist', function(req, res) {
    	getStudents(res);
    	});
    	// set the static files location /public/img will be /img for users
    	app.use(express.static(__dirname + '/public'));
    
    	// config files
    	vardb = require('./config/db');
    	console.log("connecting--",db);
    	mongoose.connect(db.url); //Mongoose connection created
    
    	// grab the student model
    	var Student = require('./app/models/student');
    	functiongetStudents(res) {
    	Student.find(function (err, students) {
    	      // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    	if (err) {
    	res.send(err);
    	         }
    	res.json(students); // return all todos in JSON format
    	      });
    	   };
    	app.get('/api/studentslist', function(req, res) {
    	getStudents(res);
    	});
    	app.post('/api/students/send', function (req, res) {
    	var student = new Student(); // create a new instance of the student model
    	   student.name = req.body.name; 
    	student.save(function(err) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	   });
    	});
    	app.delete('/api/students/:student_id', function (req, res) {
    	Student.remove({
    	      _id: req.params.student_id
    	      }, function(err, bear) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	      });
    	app.post('/api/students/send', function (req, res) {
    	var student = new Student(); // create a new instance of the student model
    	   student.name = req.body.name; 
    	student.save(function(err) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	   });
    	});
    	app.delete('/api/students/:student_id', function (req, res) {
    	Student.remove({
    	      _id: req.params.student_id
    	      }, function(err, bear) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	// set the static files location /public/img will be /img for users
    	app.use(express.static(__dirname + '/public'));
    
    	// config files
    	vardb = require('./config/db');
    	console.log("connecting--",db);
    	mongoose.connect(db.url); //Mongoose connection created
    
    	// grab the student model
    	var Student = require('./app/models/student');
    	functiongetStudents(res) {
    	Student.find(function (err, students) {
    	      // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    	if (err) {
    	res.send(err);
    	         }
    	res.json(students); // return all todos in JSON format
    	      });
    	   };
    	app.get('/api/studentslist', function(req, res) {
    	getStudents(res);
    	});
    	app.post('/api/students/send', function (req, res) {
    	var student = new Student(); // create a new instance of the student model
    	   student.name = req.body.name; 
    	student.save(function(err) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	   });
    	});
    	app.delete('/api/students/:student_id', function (req, res) {
    	Student.remove({
    	      _id: req.params.student_id
    	      }, function(err, bear) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	      });
    	// set the static files location /public/img will be /img for users
    	app.use(express.static(__dirname + '/public'));
    
    	// config files
    	vardb = require('./config/db');
    	console.log("connecting--",db);
    	mongoose.connect(db.url); //Mongoose connection created
    
    	// grab the student model
    	var Student = require('./app/models/student');
    	functiongetStudents(res) {
    	Student.find(function (err, students) {
    	      // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    	if (err) {
    	res.send(err);
    	         }
    	res.json(students); // return all todos in JSON format
    	      });
    	   };
    	app.get('/api/studentslist', function(req, res) {
    	getStudents(res);
    	});
    	app.post('/api/students/send', function (req, res) {
    	var student = new Student(); // create a new instance of the student model
    	   student.name = req.body.name; 
    	student.save(function(err) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	   });
    	});
    	app.delete('/api/students/:student_id', function (req, res) {
    	Student.remove({
    	      _id: req.params.student_id
    	      }, function(err, bear) {
    	if (err)
    	res.send(err);
    	getStudents(res);
    	      });
    	      });
    	});
    	// startup our app at http://localhost:3000
    	
    Defining Frontend Route

    index.html

    Code:-

    	
    	< html lang="en">
    	< head>
    	< meta charset="UTF-8">
    	< base href="/">
    	< title>tutorials.ducatindia.com Node and Angular< /title>
    
    	
    	< link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
    	< link rel="stylesheet" href="css/style.css">
    
    	
    	< scriptsrc="libs/angular/angular.min.js">< /script>
    	< scriptsrc="libs/angular-route/angular-route.min.js">< /script>
    
    	
    	< scriptsrc="js/controllers/MainCtrl.js">< /script>
    	< scriptsrc="js/controllers/StudentCtrl.js">< /script>
    	< scriptsrc="js/services/StudentService.js">< /script>
    	< scriptsrc="js/appRoutes.js">< /script>
    	
    	< link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
    	< link rel="stylesheet" href="css/style.css">
    
    	
    	< scriptsrc="libs/angular/angular.min.js">< /script>
    	< scriptsrc="libs/angular-route/angular-route.min.js">< /script>
    
    	
    	< scriptsrc="js/controllers/MainCtrl.js">< /script>
    	< scriptsrc="js/controllers/StudentCtrl.js">< /script>
    	< scriptsrc="js/services/StudentService.js">< /script>
    	< scriptsrc="js/appRoutes.js">< /script>
    	< scriptsrc="js/app.js">< /script>
    	< /head>
    	< body ng-app="sampleApp" ng-controller="MainController">
    	< div class="container">
    	
    	< nav class="navbarnavbar-inverse">
    	< div class="navbar-header">
    	< a class="navbar-brand" href="/">Tutorial< /a>
    	< /div>
    	< ul class="navnavbar-nav">
    	< li>< a href="/students">Students< /a>< /li>
    	< /ul>
    	< /nav>
    	
    	< link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
    	< link rel="stylesheet" href="css/style.css">
    
    	
    	< scriptsrc="libs/angular/angular.min.js">< /script>
    	< scriptsrc="libs/angular-route/angular-route.min.js">< /script>
    
    	
    	< scriptsrc="js/controllers/MainCtrl.js">< /script>
    	< scriptsrc="js/controllers/StudentCtrl.js">< /script>
    	< scriptsrc="js/services/StudentService.js">< /script>
    	< scriptsrc="js/appRoutes.js">< /script>
    	< scriptsrc="js/app.js">< /script>
    	< /head>
    	< body ng-app="sampleApp" ng-controller="MainController">
    	< div class="container">
    	
    	< nav class="navbarnavbar-inverse">
    	< div class="navbar-header">
    	< a class="navbar-brand" href="/">Tutorial< /a>
    	< /div>
    	< ul class="navnavbar-nav">
    	< li>< a href="/students">Students< /a>< /li>
    	< /ul>
    	< /nav>
    	< scriptsrc="js/app.js">< /script>
    	< /head>
    	< body ng-app="sampleApp" ng-controller="MainController">
    	< div class="container">
    	
    	< nav class="navbarnavbar-inverse">
    	< div class="navbar-header">
    	< a class="navbar-brand" href="/">Tutorial< /a>
    	< /div>
    	< ul class="navnavbar-nav">
    	< li>< a href="/students">Students< /a>< /li>
    	< /ul>
    	< /nav>
    
    	
    	< divng-view>< /div>
    	< /div>
    	< /body>
    	< /html>
    	
    The code for our controller (MainCtrl.js) is as follows:

    Code:-

    	angular.module('MainCtrl', []).controller('MainController',
    	   ['$scope','$http','Student',function($scope, $http, Student) {
    	   $scope.formData = {};
    	   $scope.loading = true;
    	   $http.get('/api/studentslist').
    	then(function(response) {
    	      $scope.student = response.data;
    	   });
    	   // CREATE 
    	   // when submitting the add form, send the text to the node API
    	   $scope.createStudent = function() {
    	      // validate the formData to make sure that something is there
    	      // if form is empty, nothing will happen
    	if ($scope.formData.name != undefined) {
    	         $scope.loading = true;
    	         // call the create function from our service (returns a promise object)
    	Student.create($scope.formData)
    	         // if successful creation, call our get function to get all the new Student
    	         .then(function (response){
    	            $scope.student = response.data;
    	            $scope.loading = false;
    	            $scope.formData = {}
    	         },    function (error){
    	         });
    	      }
    	   };
    	// UPDATE 
    	   // when submitting the add form, send the text to the node API
    	   $scope.createStudent = function() {
    	      // validate the formData to make sure that something is there
    	      // if form is empty, nothing will happen
    	if ($scope.formData.name != undefined) {
    	         $scope.loading = true;
    	         // call the create function from our service (returns a promise object)
    	Student.create($scope.formData)
    	         // if successful creation, call our get function to get all the new Student
    	         .then(function (response){
    	            $scope.student = response.data;
    	            $scope.loading = false;
    	            $scope.formData = {}
    	         },    function (error){
    	         });
    	      }
    	   };
    	   // when submitting the add form, send the text to the node API
    	   $scope.createStudent = function() {
    	      // validate the formData to make sure that something is there
    	      // if form is empty, nothing will happen
    	if ($scope.formData.name != undefined) {
    	         $scope.loading = true;
    	         // call the create function from our service (returns a promise object)
    	Student.create($scope.formData)
    	         // if successful creation, call our get function to get all the new Student
    	         .then(function (response){
    	            $scope.student = response.data;
    	            $scope.loading = false;
    	            $scope.formData = {}
    	         },    function (error){
    	         });
    	      }
    	   };
    	   // DELETE
    	   ==================================================================
    	   // delete a todo after checking it
    	   $scope.deleteStudent = function(id) {
    	      $scope.loading = true;
    	Student.delete(id)
    	      // if successful delete, call our get function to get all the new Student
    	      .then(function(response) {
    	         $scope.loading = false;
    	new list of Student
    	      });
    	   };
    	}]);

    To make API calls and execute API queries, we’ve developed a service. StudentService, our service, looks like this:

    	angular.module('StudentService', [])
    	.factory('Student', ['$http',function($http) {
    	return {
    	get : function() {
    	return $http.get('/api/students');
    	      },
    	create : function(student) {
    	return $http.post('/api/students/send', student);
    	      },
    	delete : function(id) {
    	return $http.delete('/api/students/' + id);
    	get : function() {
    	return $http.get('/api/students');
    	      },
    	update : function(student) {
    	return $http.post('/api/students/send', student);
    	      },
    	cancel : function(id) {
    	return $http.delete('/api/students/' + id);++
    	      }
    	   }
    	}]);
    	
    Running Application

    Run the command shown below from your project directory.

    $ npm start

    Go to http://localhost:3000 now. and you’ll be sent to the page seen in the image below.

    Copyright 1999- Ducat Creative, All rights reserved.