Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

The event loop and thread sturvation 


Just like in browser environment, at the heart of handling events on the server is the Event Loop, which runs in the background checking for events and executing javascript functions that are assigned to that event.

This also brings up the issue of thread starvation, known in browser also as blocking UI, which happens when there are no CPU resources left for handling additional operations.
Thread starvations happens because of heavy, long lasting operations which block the OS from assigning new threads or in nodes example blocking the single thread from reaching other tasks on the queue.
You could say that node is the wrong choice for heavy server operations, but when taking in consideration the above the same issue will be encountered on other servers as well.
The thing is, heavy lifting operation should be off-loaded to offline execution or handling of Databases in the first place.

 

The file system

Node.js uses the CommonJS module loading system. 

In CommonJS every file stands as a module on its own, and has a module object defined for it (in the scope of its execution), which can be accessed only buy the defining file. The module object has an exports object defined on it, which provides the API object of the file module.
When other files use the globally defined require() function, by providing it a path to file, the script is executed and only the module.exports object is defined.
This is desired since the module.exports also acts as a sort of a name-space, importing modules safely without name collisions.

NOTE! an import statement should provide a path that is relative, and without the .js mime type. This is done for consistency reasons between server and client, where the client does not know what file types the server is using (c#/Typescript/Js).

 

  • console
  • timers (setInterval, setTimeout, clearInterval, clearTimeout)
  • _filename, _dirname (local global, specific to file scope)
    • This variable exists in every files scope, and provide the full path to the file & directory.
  • process
    • process.argv (for command line arguments)
      • When a file is executed using the command line ( for example an NPM start script that passes parameters to an installed package) and passed parameters, these parameters can be accessed inside the file using the process.argv which is an array of strings.
        The first element of the array will be the name of the process running the script, mainly it will be "node".
        The  second element of the array will be the full file path to the script thats executing, for example '/path/to/file/on/your/filesystem/argv.js'.
        The elements following will all be the different arguments passed into the execution command.
        so an example for the returned array from process.argv  will be something like: [ 'node', '/path/to/file/on/your/filesystem/argv.js', 'foo', 'bar', 'bas' ].
        to get only the arguments you can splice the array from the 3rd element to the end of the array.
          
    • process.nextTick (event loop callback)
      • A highly efficient and simple function, that takes a callback function which is added to the start of every event loop Q execution.
        This callback function will always be the first task in an event loop Q execution.
        What important to notice about this function, is that its the first javascript function to execute in every event loop iteration, everything before it is C and everything after it is Javascrpit.
    • stdin - NOTE: RESEARCH
    • stdout - NOTE: RESEARCH
  • Buffer (global class)
    • A globally defined class used to convert data between different types of encoding, for example UTF to ASCII, or ASCII to binary. 
      example:
      var buffer = new Buffer(str, 'utf-8');
      var roundTrip = buffer.toString('utf-8');
  • global - just window object in a browser, can be accessed from anywhere in the application.