Deploy AngularJS Web App With Node.js

Introduction

The developer develops Web app and deploys them for execution on the Browser, so that the client can browse it. An application developed by using JavaScript, AngularJS, BootStrap, NodeJs is very easy to deploy. Here, I will explain the step osf deployment of AngularJs App, using NodeJs. 

Prerequisites

  1. Node.Js should be installed into your machine. 
        Source - https://nodejs.org/en/download/

Set up AngularJS Project.

The steps are given below-
  1. Create one folder named "MyProject".
  2. Create sub folder inside MyProject named "App" -- This folder will contain the project files and resources.
Take one "package.json" file inside "MyProject" Folder and put the content, as given below-
  1. {  
  2.   "name""MyProject",  
  3.   "version""0.0.1",  
  4.   "description""My Project",  
  5.   "dependencies": {  
  6.     "express""*"  
  7.   },  
  8.   "engine""node >=0.6.x",  
  9.   "scripts": {  
  10.     "start""node server.js"  
  11.   },  
  12.   "main""server.js",  
  13.   "repository": {  
  14.     "type""git",  
  15.     "url"""  
  16.   },  
  17.   "author""MyProject",  
  18.   "license""MyProject",  
  19.   "bugs": {  
  20.     "url"""  
  21.   },  
  22.   "homepage"""  
  23. }  
Take one "Server.js" file inside, as given below-
  1. var express = require('express');  
  2. var app = express();  
  3.   
  4. app.use(express.static("App"));  
  5.   
  6. app.get('/'function (req, res) {  
  7.     res.redirect('/');  
  8. });  
  9.   
  10. app.listen(8080, 'localhost');  
  11. console.log("MyProject Server is Listening on port 8080");  
Here, you can define your own port and put IP of your machine in place of "localhost".

Navigate to folder "MyProject", using command prompt like cd f:\MyProject.
 
Now, type command npm install

It will be, as given below-
 


It will install npm and create a folder with the name "node_modules". Also, it will install the dependencies, which you will put inside package.json file.
 
Now, type command npm start and it will start npm, as shown below-

 
Now, open your Browser and type - localhost:8080.
 
It will open your Web app.

Project Structure

The project structure is given below-
 
 
app.js file is an entry point of this Web app, so it should define the state, using URL router, as shown below-
  1. (function () {  
  2.     "use strict"  
  3.     var app = angular.module("myApp", ["ui.router"]);  
  4.   
  5.     app.config(["$stateProvider""$urlRouterProvider",  
  6.       function ($stateProvider, $urlRouterProvider) {  
  7.           $urlRouterProvider.otherwise("/");  
  8.           $stateProvider  
  9.             .state("home", {  
  10.                 url: "/",  
  11.                 templateUrl: "home/home.html",  
  12.                 controller: "homeController",  
  13.                 controllerAs: "homeController",  
  14.                 authenticationReqired: false  
  15.             });  
  16.       }  
  17.     ]);  
  18. }());  
Here, I define the default state "home" and its URL is "/" . In the same URL, I defined inside server.js file, as shown below- 
  1. app.get('/'function (req, res) {  
  2.     res.redirect('/');  
  3. });  

Thus, it will redirect to home state containing "/" URL on the Browser. 

Conclusion

NPM (Node Package Manager) is very good for the deployment of Web apps. It is very easy to deploy.