Quick Contact


    MEAN Stack Project Setup

    This chapter covers the creation and configuration of a MEAN application. To construct the project, we combined NodeJS and ExpressJS.

    Before we can start building a MEAN application, we must first install the necessary prerequisites.

    You may get the most recent version of Node.js by going to the Node.js website (This is for Windows users). npm is automatically installed on your machine when you download Node.js. This link will install Node and npm for Linux users.

    Using the scripts below, determine the version of Node and npm.

    • $ node –version
    • $ npm –version
    Creating Express Project

    As illustrated below, use the mkdir command to create a project directory.

    $ mkdir mean-demo /the repository’s name

    The root of the node application is the directory listed above. Run the command below to create the package.json file.

    cd webapp-demo $ cd webapp-demo

    $ npm start

    As illustrated below, use the mkdir command to create a project directory.

    $ mkdir mean-demo /the repository’s name

    The root of the node application is the directory listed above. Run the command below to create the package.json file.

    cd webapp-demo $ cd webapp-demo

    $ npm start

    The init command will guide you through the process of building a package. a json file

    This tool will guide you through the process of creating a package.json file. It just covers the most popular items and makes educated guesses as to what should be the defaults.

    See `npm help json` for definitive documentation on these fields and exactly what they do.

    Use `npm install –save` afterwards to install a package and save it as a dependency in the package.json file.

    Press ^C at any time to quit.

    name: (mean-demo) mean_tutorial

    version: (1.0.0)

    description: this is basic tutorial example for MEAN stack

    entry point: (index.js) server.js

    test command: test

    git repository:

    keywords: MEAN,Mongo,Express,Angular,Nodejs

    author: Manisha

    license: (ISC)

    About to write to /home/mani/work/rnd/mean-demo/package.json:

    
    	{
    	   "name": "mean_tutorial",
    	   "version": "1.0.0",
    	   "description": "this is basic tutorial example for MEAN stack",
    	   "main": "server.js",
    	   "scripts": {
    	      "test": "test"
    	   },
    	   "keywords": [
    	      "MEAN",
    	      "Mongo",
    	      "Express",
    	      "Angular",
    	      "Nodejs"
    	   ],
    	   "author": "Manisha",
    	   "license": "ISC"
    	}
    	
    package.json
    
    	{
    	   "name": "mean_tutorial",
    	   "version": "1.0.0",
    	   "description": "this is the sample test only for testing",
    	   "main": "server.js",
    	   "scripts": {
    	      "test": "test"
    	   },
    	   "keywords": [
    	      "MEAN",
    	      "Mongo",
    	      "Express",
    	      "Angular",
    	      "Nodejs"
    	   ],
    	   "author": "Manisha",
    	   "license": "ISC"
    	}
    	

    Use the command below to install configuration settings for the framework and configure the Express project into the current folder.

    npm install express –save

    Open the package in your project directory. You will find the following information in the json file.

    
    	{
    	   "name": "mean_tutorial",
    	   "version": "1.0.0",
    	   "description": "this is basic tutorial example for MEAN stack",
    	   "main": "server.js",
    	   "scripts": {
    	      "test": "test"
    	   },
    	   "keywords": [
    	      "MEAN",
    	      "Mongo",
    	      "Express",
    	      "Angular",
    	      "Nodejs"
    	   ],
    	   "author": "Manisha",
    	   "license": "ISC",
    	   "dependencies": {
    	"name": "mean_tutorial",
    	   "version": "1.0.0",
    	   "description": "this is the simple one text",
    	   "main": "server.js",
    	   "scripts": {
    	      "test": "test"
    	   },
    	   "keywords": [
    	      "MEAN",
    	      "Mongo",
    	      "Express",
    	      "Angular",
    	      "Nodejs"
    	   ],
    	   "author": "Manisha",
    	   "license": "ISC",
    	   "dependencies": {
    	      "express": "^4.17.1"
    	   }
    	}
    	

    You can see that an express dependency has been added to the file. The project structure is now as follows:

    -mean-demo

    Npm install creates —node modules —package.json tells npm which packages we require —server.js sets up our node application

    //Angular Starter App

    
    	var main = angular.module("main", ['ui.router','ngRoute','ngResource'])
    	.run(function($http,$rootScope)
    	{
    	    if(sessionStorage.length> 0){
    	        $rootScope.current_user = sessionStorage.current_user;
    	        $rootScope.authenticated = true;
    	    }else{
    	        $rootScope.authenticated = false;
    	        $rootScope.current_user = 'Guest';
    	    }
    
    	    $rootScope.signout = function(){
    	        $http.get('auth/signout');
    	        $rootScope.authenticated = false;
    	        $rootScope.current_user = 'Guest';
    	sessionStorage.clear();
    	    };
    
    	});                                                                                                     
    	//Routing Configuration (define routes)
    	main.config([
    	    '$stateProvider', '$urlRouterProvider', '$httpProvider',
    	    function ($stateProvider, $urlRouterProvider,$rootScope) {
    	        $urlRouterProvider.otherwise('/');
    	        $stateProvider
    	            .state('home', {
    	                url: '/',
    	templateUrl: 'Index.html',
    	caseInsensitiveMatch: true,
    	                controller: 'MainController'
    	            })
    	            .state('contact', {
    	                url: '/contact',
    	templateUrl: 'Contact.html',
    	caseInsensitiveMatch: true,
    	                controller: 'MainController'
    	            })
    	            .state('about', {
    	                url: '/about',
    	templateUrl: 'About.html',
    	caseInsensitiveMatch: true,
    	                controller: 'MainController'
    	            })
    
    	    '$stateProvider', '$urlRouterProvider', '$httpProvider',
    	    function ($stateProvider, $urlRouterProvider,$rootScope) {
    	        $urlRouterProvider.otherwise('/');
    	        $stateProvider
    	            .state('home', {
    	                url: '/',
    	templateUrl: 'Index.html',
    	caseInsensitiveMatch: true,
    	                controller: 'MainController'
    	            })
    	            .state('contact', {
    	                url: '/contact',
    	templateUrl: 'Contact.html',
    	caseInsensitiveMatch: true,
    	                controller: 'MainController'
    	            })
    	            .state('about', {
    	                url: '/about',
    	templateUrl: 'About.html',
    	caseInsensitiveMatch: true,
    	                controller: 'MainController'
    	            })
    	            .state('login',{
    	                url: '/login',
    	templateUrl: 'login.html',
    	caseInsensitiveMatch: true,
    	                controller: 'AuthController'
    	            })
    	            .state('register',{
    	                url: '/register',
    	templateUrl: 'register.html',
    	caseInsensitiveMatch: true,
    	                controller: 'AuthController'
    	            }).state('unauth',{
    	                url: '/unauth',
    	templateUrl: 'unauth.html',
    	caseInsensitiveMatch: true
    	            });
    	            .state('login',{
    	                url: '/login',
    	templateUrl: 'login.html',
    	caseInsensitiveMatch: true,
    	                controller: 'AuthController'
    	            })
    	            .state('register',{
    	                url: '/register',
    	templateUrl: 'register.html',
    	caseInsensitiveMatch: true,
    	                controller: 'AuthController'
    	            }).state('unauth',{
    	                url: '/unauth',
    	templateUrl: 'unauth.html',
    	caseInsensitiveMatch: true
    	            });
    	    }
    	]);
    	

    Now, under the Angular/Models folder, add a Model user.js.

    //create new model

    var mongoose = require(‘mongoose’); //refering mongoose for creating user friendly class type model.

    //defining schema for user model

    
    	varuserSchema = new mongoose.Schema({
        username: String,
        password: String,
        email: String,
        role: String,
    	created_at: {type: Date, default: Date.now}
    
    	});
    	mongoose.model('User', userSchema);
    	var User = mongoose.model('User');
    	exports.findByUsername = function(userName, callback){
    	User.findOne({ user_name: userName}, function(err, user){
    	        if(err){
    	            return callback(err);
    	mongoose.model('User', userSchema);
    	var User = mongoose.model('User');
    	exports.findByUsername = function(userName, callback){
    	User.findOne({ user_name: userName}, function(err, user){
    	        if(err){
    	            return callback(err);
    	        }
    	return callback(null, user);
    	});
    	}
    
    	exports.findById = function(id, callback){
    	User.findById(id, function(err, user){
    	        if(err){
    	           return callback(err);
    	mongoose.model('User', userSchema);
    	var User = mongoose.model('User');
    	exports.findByUsername = function(userName, callback){
    	User.findOne({ user_name: userName}, function(err, user){
    	        if(err){
    	            return callback(err);
    	        }
    	return callback(null, user);
    	});
    	}
    
    	exports.findById = function(id, callback){
    	User.findById(id, function(err, user){
    	        if(err){
    	           return callback(err);
    	        }
    	return callback(null, user);
    	});
    	}
    
    	exports.findById = function(id, callback){
    	User.findById(id, function(err, user){
    	        if(err){
    	           return callback(err);
    	        }
    	         return callback(null, user);
    	    });
    	}
    	

    Let’s make some renderable views. Create a new folder Views and add the following ejs code to the main directory, as ejs is the render engine used by nodejs in the Starter.ejs file.

    
    	< html ng-app="main">
    	< head>
    	< meta charset="utf-8" />
    	< meta name="viewport" content="width=device-width, initial-scale=1.0">
    	< meta name="viewport" content="width=device-width" />
    	< title>Super Application< /title>
    	< link href="bootstrap.css" rel="stylesheet" />
    	< link href="Site.css" rel="stylesheet" />
    	< script src="modernizr-2.6.2.js">< /script>
    	< script src="jquery-1.10.2.js">< /script>
    	< script src="bootstrap.js">< /script>
    	< script src="angular.js">< /script>
    	< script src="angular-route.js">< /script>
    	< script src="angular-ui-router.js">< /script>
    	< script src="angular-resource.js">< /script>
    	< script src="/Modules/mainApp.js">< /script>
    	< script src="/Controllers/MainController.js">< /script>
    	< script src="/Controllers/AuthController.js">< /script>
    	< /head>
    	< body>
    	< div class="navbarnavbar-inverse navbar-fixed-top">
    	< div class="container">
    	< div class="navbar-header">
    	< button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
    	< span class="icon-bar">< /span>
    	< span class="icon-bar">< /span>
    	< span class="icon-bar">< /span>
    	< /button>
    	< a class="navbar-brand" href="#/home">Application name< /a>
    	< /div>
    
    	< div class="navbar-collapse collapse">
    	< ul class="navnavbar-nav">
    	< li>< a href="#/home">Home< /a>< /li>
    	< li>< a href="#/about">About< /a>< /li>
    	< li>< a href="#/contact">Contact< /a>< /li>
    	< /ul>
    	< ul class="navnavbar-navnavbar-right">
    	< li>< p class="navbar-right navbar-text">Signed in as {{current_user}}< /p>
    	< li>< p class="navbar-right navbar-text" ng-hide="authenticated">
    	< a href="#/login">Login< /a> or < a href="#/register">Register< /a>
    	< /p>< /li>
    	< li>< p class="navbar-right navbar-text" ng-show="authenticated">
    	< a href="#" ng-click="signout()">Logout< /a>
    	< /p>< /li>
    	< /ul>
    	< /div>
    	< /div>
    	< /div>
    	< div class="container body-content">
    	< div ui-view>
    	< /div>
    	< div class="navbar-collapse collapse">
    	< ul class="navnavbar-nav">
    	< li>< a href="#/home">Home< /a>< /li>
    	< li>< a href="#/about">About< /a>< /li>
    	< li>< a href="#/contact">Contact< /a>< /li>
    	< /ul>
    	< ul class="navnavbar-navnavbar-right">
    	< li>< p class="navbar-right navbar-text">Signed in as {{current_user}}< /p>< /li>
    	< li>< p class="navbar-right navbar-text" ng-hide="authenticated">
    	< a href="#/login">Login< /a> or < a href="#/register">Register< /a>
    	< /p>< /li>
    	< li>< p class="navbar-right navbar-text" ng-show="authenticated">
    	< a href="#" ng-click="signout()">Logout< /a>
    	< /p>< /li>
    	< /ul>
    	< hr />
    	< footer>
    	< p>My ASP.NET Application< /p>
    	< /footer>
    	< /div>
    	< div class="navbar-collapse collapse">
    	< ul class="navnavbar-nav">
    	< li>< a href="#/home">Home< /a>< /li>
    	< li>< a href="#/about">About< /a>< /li>
    	< li>< a href="#/contact">Contact< /a>< /li>
    	< /ul>
    	< ul class="navnavbar-navnavbar-right">
    	< li>< p class="navbar-right navbar-text">Signed in as {{current_user}}< /p>< /li>
    	< li>< p class="navbar-right navbar-text" ng-hide="authenticated">
    	< a href="#/login">Login< /a> or < a href="#/register">Register< /a>
    	< /p>< /li>
    	< li>< p class="navbar-right navbar-text" ng-show="authenticated">
    	< a href="#" ng-click="signout()">Logout< /a>
    	< /p>< /li>
    	< /ul>
    	< /body>
    	< /html>
    	

    Copyright 1999- Ducat Creative, All rights reserved.