Node.js Quick Reference Guide

Node.js is not a programming language. It’s not even a framework. It’s a runtime environment, written in C++,which uses Google Chrome’s V8 engine. It was devised to run JavaScript outside of Web Browsers on backend servers.Web browsers have JavaScript engines, which allow execution of JavaScript codes inside them.Internet Explorer has Chakra, Mozilla has SpiderMonkey and Google Chrome has V8 engine for JavaScript execution in these browsers.

Node.js uses single thread. That single thread serves different i/o requests. While processing for one i/o request is going on, our single thread can go and serve other i/o request.Node server can be used for Data/I/O related purposes. But not in scenarios where we need to have bigger computational power of CPU.By default, Node is asynchronous. But it can works both ways - synchronous as well as asynchronous.

When executing inside web browsers, JavaScript has ‘window’ as the global variable defined.This window variable can be used to refer to other components defines within the JavaScript code.While executing JavaScript inside Node, we have ‘global’ as the global variable, which can be used to refer tocomponents defined within the JavaScript code. As always, dropping the global/window variable is a shorthand and that’s which is widely used by developers.

Exporting and Loading of modules in Node.We use module.exports for exposing the components to be used by other js files in Node.We use require(), in order to import the exported modules.In modules, we can exports a single function or an object.‘module’ is one such variable which cannot be accessed by ‘global’ variable inside node.

Node has some inbuilt modules like fs(FileStream), http etc. which serves our purpose to a great extent and allows us to use them, without having to worry about writing the logic on our own.

Module Wrapper Function: Node doesn’t execute our code directly. Each file with snippets of code directly. It wraps everything inside of a function, which has parameters-exports, require, module, filename, dirname.Inside this function, our code executes. This function is called Module Wrapper Function. So by looking at the arguments, we can say that ‘require’ seems to be global, but its not. It is local to a Module Wrapper Function.filename is the variable which refers to the current filename’s fully qualified path.
dirname is the fully qualified path to the current file’s directory.

Events module, and the EventEmitter class has a very high importance because of the potential that these give to developers:

12345

cont EventEmitter = require('events');// EventEmitter is a class.// we need to create instance of this class in order to use its methods.const emitter = new EventEmitter();emitter.emit("message", {id:1, url:'http://anshulgautam.in'}); // signalling that an event has happened.

Nowadays, to handle various routes, we use a framework called Express, which handles such routes easily and in a cleaner way. Express is built on top of HTTP module in Node.That will be part of another post on this site. =)