EventEmitter is a Node native class containing a set of methods to operate on the 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()})

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.

// 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");

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

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.

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;

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.


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

console.log("Module directory: " + __dirname);

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

console.log("Filename: " + __filename);

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

node --version

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

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); 


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.

const path = require('path');
var pathData = path.parse(__filename);

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.

const fs = require('fs');

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

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.

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

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

node index.js
Sitemap Git repository