EventEmitter is a Node native class containing a set of methods to operate on the events.

#node#module#events
const EventEmitter = require('events');
const emitter = new EventEmitter();

// Set an event listener
emitter.on('userSubscribed', function(eventArg){
    console.log("User subscribed at ", eventArg.date);
    // Do something when user subscribes
});

// Raise an event and pass data as the second argument
emitter.emit('userSubscribed', {date: Date.now()})
copy

Node comes with a full support for setting up a HTTP server. With a few lines of code you can build a simple web server.

#node#module#http
// Load HTTP module
const http = require('http');

// Create an instance of a server
const server = http.createServer((req, res) => {
    // Set a route
    if (req.url === '/') {
        // Send back the response back
        res.write("Node HTTP module in action");
        res.end();
    }
});

// Start the server on a port 3000
server.listen(3000);
console.log('Server listening on port 3000');
copy

Each file in your Node application represents a module and it has its own scope. Therefore to access data or functionality from another file/module you need to make them public within your application. To do that use Export method on the Module object.

#node#module
var settings = {
    colour: "green",
    active: true,
    number: 43
};

module.exports.data = settings; // This exports an object containing the settings object

// When exporting a single function or an object you can simplify the export to the following.
// module.exports = settings;
copy

Global object is an equivalent of the Window object available in the browser environment. The main difference between the two is that new variables and functions created in the top scope of the application are added to the Window object in the browser environment but not in Node. In Node, they are scoped to the file (module) they have been declared in.

#node
global
copy

Dirname argument belongs to module wrapper function and it stores the absolute path to the module.

#node#module
console.log("Module directory: " + __dirname);
copy

Filename argument belongs to module wrapper function and it stores the module name including the absolute path to the file.

#node#module
console.log("Filename: " + __filename);
copy

Run this command to check Node version. You can also use the shorthand "-v".

#node
node --version
copy

OS module is a native Node module giving you access to operating system information like memory, architecture, uptime and many more.

#node#module
const os = require('os'); // Node native module

function logOsData(){

    console.log(`Architecture: ${ os.arch() }`);
    console.log(`Total memory: ${ os.totalmem() }`);
    console.log(`Free memory: ${ os.freemem()} `);
    console.log(`Platform: ${ os.platform()} `);
    console.log(`Type: ${ os.type()} `);
    console.log(`Uptime: ${ os.uptime()} `);

    // Uncomment the line below to log full OS object in the console.
    // console.log(os); 
}

logOsData();
copy

Parse a method belonging to the Path native Node module. A parsed path returns an object containing directory, module base name, extension and the file name.

#node#module#path
const path = require('path');
var pathData = path.parse(__filename);
console.log(pathData);
copy

Read directory (readdir) is a method belonging to the Node native file system module (fs). reddir is an asynchronous method and it used a javascript promise to return an error or an array with a list of filenames located in the given directory.

#node#module
const fs = require('fs');

fs.readdir('./', function(err, files){
    console.log("--- FS INFO");
    if (err) console.log("Error: ", err);
    else console.log("Root: ", files);
});
copy

Require is a Node specific function letting you add exported functionality and/or data from other modules. The function takes an argument for the module location. The ".js" file extension is optional. It's also a good practice to use constants when assigning required modules.

#node#module
const settings = require('./settings.js');
// This works the same way
const settings = require('./settings');
copy

To start a script in node simply point to the file.

#node
node index.js
copy