Search This Blog

2015-11-01

AngularJS

AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop.

It could be downloaded from https://angularjs.org/

Sample Application:

1)Create 3 file index.html,app.js and appcontroller.js

2)index.html:
<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="app.js"></script>
    <script src="appcontroller.js"></script>
</head>
<body>
    <form ng-app="myApp" ng-controller="customersCtrl" name="myForm" novalidate>
<!-- ng-app Defines the root element of an application.  -->
        <ul>
            <li ng-repeat="x in names| orderBy : 'Country'"> <!-- ng-repeat : Defines a template for each data in a collection.  -->
               <!-- AngularJS Filters:
                currency- Format a number to a currency format.
                filter- Select a subset of items from an array.
                lowercase- Format a string to lower case.
                orderBy- Orders an array by an expression.
                uppercase- Format a string to upper case.  -->
                <span ng-show="x.Country != 'France'"> <!-- ng-show : Shows or hides HTML elements.  -->

                    {{ x.Name +' , '+ x.City +', ' + x.Country|uppercase }}
                <input type="text" ng-model="x.City"> <!-- ng-model: Binds the value of HTML controls to application data.  -->
               
                    <button ng-click="getCity(x.City)">Click Me</button>
                    <!-- AngularJS support the following events:
                    •ng-click
                    •ng-dbl-click
                    •ng-mousedown
                    •ng-mouseenter
                    •ng-mouseleave
                    •ng-mousemove
                    •ng-keydown
                    •ng-keyup
                    •ng-keypress
                    •ng-change -->


                </span>
            </li>
        </ul>
        <p>Email:<br>
          <input type="email" name="email" ng-model="email" id="myemail" required>
          <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
          <span ng-show="myForm.email.$error.required">Email is required.</span>
          <span ng-show="myForm.email.$error.email">Invalid email address.</span>
          </span>
        </p>
        <!-- Validation
        $dirty- The user has interacted with the field.
        $valid- The field content is valid.
        $invalid- The field content is invalid.
        $pristine- User has not interacted with the field yet.  -->

     
    </form>

</body>
</html>


2)app.js

var app = angular.module('myApp', []); 
// angular.module() -Creates, registers, or retrieves an AngularJS module

3)appcontroller.js

app.controller('customersCtrl', function ($scope, $http) {
   
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function (response) { $scope.names = response.records; });
   
    $scope.getCity = function (input) {
        alert(input);
    }
});



2015-10-26

Serving a HTML file from Node JS

We can serve static files from the HTTP servers that we create using Node.js.

we are going to take advantage of two modules to do that. Those are the "connect" module and the "serve-static" module. So "connect" is an extensible HTTP framework that allows me to plug in additional functionality into my HTTP server and "serve-static" is one of the plug-ins that we are going to use to help us serve static files easily from our applications.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below and run 'npm install'

{
  "name": "demostaticfile",
  "version": "1.0.0",
  "description": "Service Static Content from web server",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MRB",
  "license": "ISC",
  "dependencies" : {
    "connect"         : "*",
    "serve-static"    : "*"
   
  }
}


Step 4: Create a folder 'public' under the root and place html and image files within the public folder.

Step 5: Write below code in your index.js

var connect=require("connect");
var serveStatic=require("serve-static");

var app=connect()
  .use(serveStatic('public'))
  .use(function  (req,res) {
   res.end("Welcome to the Static Demo");
  })
  .listen(7878);

console.log("listening from port 7878");

Step 6: Open your browser and try browsing your test html file or image file

http://localhost:7878/test.html

2015-10-23

Creating a Node.js HTTP Client Application

This is useful when you are providing some sort of a proxy functionality or even some sort of web scraping type functionality where you are going to go out and grab web pages, and then attempt to process the HTML page, and get some information from it.

we are actually going to be using an external 'request' module.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below and run 'npm install'

"dependencies": {
"request": "*"
}


Package.json:

{
  "name": "demohttpclientapp",
  "version": "1.0.0",
  "description": "create a http client application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MRB",
  "license": "ISC",

  "dependencies": {
  "request": "*"
   }
}


Step 4:Write below code in your index.js

var request = require('request');
request('http://www.google.com', function(error, response, body){
if (!error && response.statusCode == 200){
console.log(body);
}
if (error){
console.log(error);
}
});

Step 5: Run the application by 'node index.js' and see the responses in console.

Configure responses from a Node.js web server

we can customize or configure the type of response that we send back in our application. And to help us in this demo, we are going to be using the "connect" module, which is a framework that's going to allow us to easily plug in functionality to our HTTP server.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below

"dependencies": {
"Connect": "*"
}


Step 4 :

var connect=require('connect');

var app=connect()
.use(function(req,res){


 if(req.url == "/hello")
 {
  console.log("sending plain");
  res.end("Hello from app");
 }

 else if(req.url == "/hello.json")
 {
  console.log("sending json");
  var data = "Hello";
  var jsonData = JSON.stringify(data);
  res.setHeader('Content-Type', 'application/json');
  res.end(jsonData);
    }

 else if(req.url == "/hello404")
 {
  console.log("sending 404 status code")
  res.statusCode = 404;
  res.end("Oops, could not find something");
 }

res.end("I am listening");

})
.listen(1111);

console.log("listening at port 1111");

Step 5: Run the application by 'node index.js' and try 'http://localhost:1111/hello' in browser