Skip to content

Instantly share code, notes, and snippets.

View tarekahsan709's full-sized avatar
:octocat:

Tarek Ahsan tarekahsan709

:octocat:
View GitHub Profile
@tarekahsan709
tarekahsan709 / cluster.md
Created October 5, 2017 11:26 — forked from learncodeacademy/cluster.md
Node Cluster - Enhance your node app by using all the cores of your processor.

Here's all you have to do to add clustering to your node.js application.

  • save this code as cluster.js, and run cluster.js instead of server.js (or /bin/www, or whatever it's called for your project)
  • the only line you'll need to change is the last line - it needs to point to the location of your server.js file
var cluster = require('cluster');

if (cluster.isMaster) {
  // Count the machine's CPUs
 var cpuCount = require('os').cpus().length;

Some basic node js knowledge.

https://docs.nodejitsu.com/articles/getting-started/

What is REPL(Read, Eval, Print, Loop) ?

A Read-Eval-Print Loop (REPL) is an interactive interpreter to a programming language. https://stackoverflow.com/questions/13603021/what-is-a-repl-in-javascript

And for node it is the Node.js shell; any valid Javascript which can be written in a script can be passed to the REPL. It can be extremely useful for experimenting with node.js, debugging code, and figuring out some of Javascript's more eccentric behaviors. https://nodejs.org/api/repl.html

Why Node js ?

We were developing a real time application, which need a persistent connection with ther server. Nodejs does not create a new connection for every request. One thread per connection (blocking I/O or non-blocking I/O) requires more memory resources (also kernel memory) is a disadvantage. And every additional thread means more work for the scheduler.

How the single threaded non blocking IO model works in Node.js

The program code running in nodejs thread is excuted synchronously but Whenever an I/O request come node queue the request to the event loop along with a callback function. And Libev is the event loop which actually runs internally in node.js to perform simple event loop operations. LibUv performs, mantains and manages all the io and events in the event pool. ( in case of libeio threadpool ). So outside of the main thread, libev and libeio handle it in the thread pool and libev provide the interaction with the main loop. https://stackoverflow.com/questions/10680601/nodejs-event-l

AngularJS Back To Top Directive.
Uses AngularJS, jQuery, Font Awesome, and SCSS.

We can add getter & setter in javaScript object like normal properties but secretly have methods associated with them.

var Student = {
	name: "Default",
	type: "Student",
 	get Name(){
  	return this.name;},
	set Name(name){
	 this.name = name;
 },

We can create object in javascript using constructor. By conention constructor name should be in uppercase. The created object will get all the define property as well as a hidden property called prototype. Which is also a object and it's value either null or object. The constructor will have its this variable bound to a newly created fresh object.

function Student (name, id) {
  this.name = name;
  this.id = id;
  this.details = function (){
    return "Name of the object " + this.name + " and it's id " + this.id;
  }
}

In JavaScript, scope is the set of variables, objects, and functions that you have access. In the other hand current context of your code.

Scope can be global, local, functional, public, private and lexical.

Lexical scope - A function within another function, the inner function has access to the scope in the outer function, this is called Lexical Scope or Closure - also referred to as Static Scope.

// Scope A
var myFunction = function () {
  // Scope B
  var name = 'Todd'; // defined in Scope B
  var myOtherFunction = function () {
 // Scope C: `name` is accessible here!

The difference between angular js service and factory.

Service and Factory both just a simple function. Service acts as a constructor function but Factory is really just a function that gets called, which is why we have to return an object explicitly. We can create & return anything that's why Factory is much more powerful and flexible.

reference: https://toddmotto.com/factory-versus-service

Here's example of Service

app.service('MyService', function () {
  this.sayHello = function () {
    console.log('hello');
  };