Full Trust European Hosting

BLOG about Full Trust Hosting and Its Technology - Dedicated to European Windows Hosting Customer

Node.js Hosting Russia - HostForLIFE.eu :: How to Clustered HTTP Server in Node.js ?

clock February 24, 2015 07:39 by author Peter

In this short article, let me tell you about How to Clustered HTTP Server in Node.js. Now this is the code that I ued to clustere the HTTP Server:

function () {
    'use strict';
        var cluster = require('cluster'),
        http = require('http'),
        os = require('os'),       
        /*
         * ClusterServer object
         *
         * We start multi-threaded server instances by passing the server object
         * to ClusterServer.start(server, port).
         *
         * Servers are automatically started with a number of threads equivalent
         * to the number of CPUs reported by the os module.
         */
        ClusterServer = {
            name: 'ClusterServer',           
            cpus: os.cpus().length,           
            autoRestart: true, // Restart threads on death?
                        start: function (server, port) {
                var me = this,
                    i;                
                if (cluster.isMaster) { // fork worker threads
                    for (i = 0; i < me.cpus; i += 1) {
                        console.log(me.name + ': starting worker thread #' + i);
                        cluster.fork();
                    }                   
                    cluster.on('death', function (worker) {
                        console.log(me.name + ': worker ' + worker.pid + ' died.');
                        if (me.autoRestart) {
                            console.log(me.name + ': Restarting worker thread...');
                            cluster.fork();
                        }
                    });
                } else {
                    server.listen(port);
                }
            }
       },       
        /*
         * Simple example HelloWorld HTTP server
         *
         * Repsonds to any request with a plain txt "Hello World!" message.
         *
         * You can replace this with much more complex processing, naturally.
         */
        helloWorldServer = http.createServer(function (request, response) {
            response.writeHead(200, {
                'Content-type': 'text/plain'
            });
                        response.end('Hello World!');
            console.log('helloWorldServer: Served a hello!');
        });   
    ClusterServer.name = 'helloWorldServer'; // rename ClusterServer instance    ClusterServer.start(helloWorldServer, 8081); // Start it up!
}());

I hope it works for you!

HostForLIFE.eu Node.js Hosting

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



AngularJS Hosting - HostForLIFE.eu :: How to Create Alert Box with AngularJS?

clock February 10, 2015 17:14 by author Peter

In this short tutorial, I will show you how to create an alert box using AngularJS. There is the difference between JavaScript and AngularJS. In AngularJS "$window" is need to referring to global Window Objects. If you want to show the alert in Angular then you need to pass it as $window.alert. First step is you need to add an external Angular.js file to your app. You need to visit the AngularJS website. After downloading the external file, you should add the following code to the Head section of your application.

 

 <head runat="server">
    <script src="angular.min.js"></script>
    <title></title>
</head>

Next step, I will create a function and write this function in the head section of your apps:
<script>
        function greet($window, $scope) {
            $scope.hello = function () {
                $window.alert('Hi!! ' + $scope.name);
            }
        }
</script>

We created a function named "greet", in $scope function and $window are passed as objects as a result of scope is used to pass a value to the controller and as you recognize we are going to show an alert window. that's why $window is used.

An alert is used but have $window before it, during this alert the message "Hi!!" are displayed at the side of some value that will be passed through a TextBox. This will be done using the $scope.name. Now you'll be thinking that I have neither declared name anyplace nor provided an initial value to the scope, you'll pass the initial value before this hello function however that may solely help to point out some text by default and nothing much more than that.

Now write this code in the body section:
  <body>
    <div ng-controller="greet">
      Name: <input ng-model="name" type="text"/>
      <button ng-click="greet()">Greet</button>
    </div>
  </body>

Here first I have applied a controller to a div, then an input tag is used in which "name" is applied through the ng-model, thus no matter is entered into the TextBox are shown within the alert box on the click of a button. The button click is sure to greet using the ng-click(). currently our work on this application is completed and it's complete code is as follows:
<head runat="server">
    <script src="angular.min.js"></script>
    <title></title>
    <script>
        function greet($window, $scope) {
            $scope.hello = function () {
                $window.alert('Hi!! ' + $scope.name);
            }
        }
    </script>
</head>
    <body>
    <div ng-controller="greet">
      Name: <input ng-model="name" type="text"/>
      <button ng-click="greet()">Greet</button>
    </div>
  </body>
</html>

On running the application you will see a TextBox and a Button.

If now I click on the Button then an Alert message will be shown but in the alert box "Hello Undefined" will be shown because nothing is provided through the TextBox and no initial value was passed through the $scope.

HostForLIFE.eu AngularJS Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



Node.js Hosting Italy - HostForLIFE.eu :: How to Use Inheritance in Node.js ?

clock February 3, 2015 06:52 by author Peter

In this post we'll take a glance on inheritance of objects in Node.js. we'll learn to use utility module to achieve inheritance in Node.js. but confine mind that you simply will write plain JavaScript to attain inheritance in Node also.

You'll use Object.create() to inherit one object to a different in Node.js also.

In this post we'll learn to use util module to achieve inheritance in Node.js. First, you should to import util module in your application.
var util= require(‘util’);

After importing util module, allow us to say you have got an object as below,
function Student()

 this.name = "G Block";
 this.age = 40;
};


Just for demonstration allow us to add function in object using prototype,
Student.prototype.Display= function(){
 console.log(this.name + " is " + this.age + " years old");
 };


Next we tend to we progressing to make ArtsStudent object which is able to inherit Student object.
function ArtsStudent()
{
 ArtsStudent.super_.call(this);
 this.subject = "music";
 }; 
util.inherits(ArtsStudent,Student);


Second line of code in ArtsStudent object is very important,
ArtStudent.super_.call(this);

If you don’t call constructor of parent object as shown in above code snippet then on making an attempt to access properties of parent object will come undefined. In last line ArtStudent inherits Student using util.inherits() function ,

util.iherits(ArtsStudent,Student);

Next you can create instance of ArtsStudent and call function of parent object as below,
var a = new ArtsStudent();
a.Display();


Inheritance will be chained to any order. If you want you can inherit object from ArtsStudent as well. Inherited object will contain properties from both ArtsStudent and Student objects. So let us consider one more example,
function ScienceStudent()
{
 ScienceStudent.super_.call(this);
 this.lab = "Physics";
}
util.inherits(ScienceStudent,ArtsStudent);
var b = new ScienceStudent();
b.Display();

On this example ScienceStudent object inherits both Student and ArtsStudent objects. With this example, you can work with inheritance in Node.js using util module. I hope it works for you!

HostForLIFE.eu Node.js Hosting

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.

 



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2012 Hosting, ASP.NET 4.5 Hosting, ASP.NET MVC 4 Hosting, and SQL 2014 Hosting.

Free ASP.NET Hosting - Europe

Tag cloud

Sign in