Nodejs : Simple get call using http node module

var http = require('http');

var options = {
  host: 'example.com',
  port: 80,
  path: '/foo.html'
};

http.get(options, function(resp){
  resp.on('data', function(chunk){
    //do something with chunk
  });
}).on("error", function(e){
  console.log("Got error: " + e.message);
});
Advertisements

AngularJS 2.0 : Environment setup

Here are simple steps you need to follow to get Angular Js 2.0 application up and running on your box.

# Make sure you have latest nodejs download link>   https://nodejs.org/en/

# Using npm install type script from command line>  npm install g typescript

# Download latest angular cli too > npm install g angularcli@latest

You are all set up create your new dream app, Angular CLI tool would create the basic structure required for a new app, since you have environment ready, lets create your dream app and run it on cli server

# Run Below commands, it would create you app.

$ng new my-dream-app

$cd my-dream-app

$ng serve

 

 

You might want to download atom IDE with supporting package from  https://atom.io/

NodeJs : Convert json to csv using node module

Its very common usecase to convert json docs or Mongo doucments into csv and share with other. I am sharing a simple node module with example to accomplish the same.

Details of package - https://www.npmjs.com/package/json2csv

# To install node module from cmd 
npm install json2csv --save



# create a converter.js javascript file as below with you input json and mapping.

var json2csv = require('json2csv');
var fs = require('fs');
var fields = ['car.make', 'car.model', 'price', 'color'];
var myCars = [

    {
        "car": {
            "make": "Hundai", "model": "sonata"
        },
        "price": 30000,
        "color": "white"
    },
    {
        "car": {
            "make": "Audi", "model": "A3"
        },
        "price": 40000,
        "color": "blue"
    }, {
        "car": {"make": "BMW", "model": "F20"},
        "price": 35000,
        "color": "black"
    }, {
        "car": {"make": "Porsche", "model": "9PA AF1"},
        "price": 60000,
        "color": "green"
    }
];
var csv = json2csv({ data: myCars, fields: fields });

fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});


# now run javascript file on node
node converter.js

# This will create a csv file in the current directory called as file.csv open it in excell and you are done.

Protractor : End-to-end test framework for AngularJS applications

Protractor runs tests against your application running in a real browser, interacting with it as a user would.

The Protractor documentation for users is located in the protractor/docs folder.

To get set up and running quickly:

 

API : What is REPOSE and how it secures restful apis

Repose provides solutions to API processing tasks such as authentication, rate limiting, API validation, HTTP Request logging, and much more. It is an open-source RESTful middleware platform that is highly scalable and extensible.It takes incoming requests and adjusts them to be ready for consumption by passing requests through a series of extensible filters.

repose

 

Repose enables downstream services to trust incoming requests as validated and well formed.
Here is link of Repose framework  :- http://www.openrepose.org/

Splice to add, replace and remove javascript array

Here is working code link : http://plnkr.co/edit/SHfpFQ?p=preview

Splice is an effective way to add, replace or remove elements from specific index in Javascript Array.

Javascript array splice() method changes the content of an array, adding new elements while removing old elements.

Syntax

Its syntax is as follows −

array.splice(index, howMany, [element1][, ..., elementN]);

Parameter Details

  • index − Index at which to start changing the array.
  • howMany − An integer indicating the number of old array elements to remove. IfhowMany is 0, no elements are removed.
  • element1, …, elementN − The elements to add to the array. If you don’t specify any elements, splice simply removes the elements from the array.

Return Value

Returns the extracted array based on the passed parameters.

If you specify a different number of elements to insert than the number you’re removing, the array will have a different length at the end of the call.

Examples

Using splice()

The following script illustrates the use of splice():

var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];

// removes 0 elements from index 2, and inserts 'drum'
var removed = myFish.splice(2, 0, 'drum');
// myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon']
// removed is [], no elements removed

// removes 1 element from index 3
removed = myFish.splice(3, 1);
// myFish is ['angel', 'clown', 'drum', 'surgeon']
// removed is ['mandarin']

// removes 1 element from index 2, and inserts 'trumpet'
removed = myFish.splice(2, 1, 'trumpet');
// myFish is ['angel', 'clown', 'trumpet', 'surgeon']
// removed is ['drum']

// removes 2 elements from index 0, and inserts 'parrot', 'anemone' and 'blue'
removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue');
// myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon']
// removed is ['angel', 'clown']

// removes 2 elements from index 3
removed = myFish.splice(3, Number.MAX_VALUE);
// myFish is ['parrot', 'anemone', 'blue']
// removed is ['trumpet', 'surgeon']

AngularJs : Inheritance in Controllers

Here is link of working example : http://jsfiddle.net/premaseem/jwenampL/2/

In order to take advantage of inheritance of scope in Nested controllers, one has to define Controllers one into another using ng-controller attribute. Sometimes you don’t want to define controllers like this but still want to use power of inheritance within controllers. May be you want to put common logic into BaseController and use it in all the child controllers.

In order to achieve this, we must use $injector object that AngularJS provides.

function BMWController($scope, $injector) {

$injector.invoke(CarController, this, {$scope: $scope});

$scope.name = 'BMW';

}