Skip to content

Node JS

Sandesh Kota edited this page Dec 11, 2019 · 56 revisions
  • A javascript runtime environment that executes the Javascript code outside of a browser. It can run on various platforms (Windows, Linux, Unix, Mac OS X, etc..)
  • Acts as a Web Server, Server Language (Code), Tools (comes with NPM - Package Manager, Module Dependency Manager)
  • Asynchronous operations without Threading (Single thread handles by using callback functions)
  • Node JS is managed by Event Loop (Libuv Library), using Callback
    • When Open a file is fulfilled by OS, it triggers an event which is handled by Event Loop
    • An incoming HTTP request triggers an event which is handled by Event Loop
    • Timers trigger event which is handled by Event Loop

Synchronus v/s Asynchronus

  • Synchronus
function serveCustomer(customer) {
    let order = customer.placeOrder(menu);
    let food = cook.prepareFood(order);
    let tip = customer.eatAndPay(food);
    return tip;
}
  • Asynchronus: Node Style
function serveCustomer(customer, done) {
    customer.placeOrder(menu, (error, order) => {
        cook.prepareFood(order, (error, food) => {
            customer.eatAndPay(food, done);
        });
    });
}
// also called as error first callback pattern - you can see the callback methods has error as the first parameter

// better way using promises
function serveCustomer() {
  return customer.placeOrder(menu)
    .then(order => cook.prepareFood(order))
    .then(food => customer.eatAndPay(food));
}
// better way using async & await
function serveCustomer = async(customer) => {
  let order = await customer.placeOrder(menu);
  let food = await cook.prepareFood(order);
  let tip = await customer.eatAndPay(food);
  return tip;
}

Event Emitter : Built on Event Loop & asynchronus concepts

const EventEmitter = require('events');
const emitter = new EventEmitter();

// emitter.emit()
emitter.on('data', (msg) => {
  console.log(msg);
});

//emitter.on()
emitter.emit('data', 'Hello World!');
  • For Example above
const serveCustomer = (customer, done) => {
  cusotmer.on('decided', order => {
    order.on('prepared', food => customer.eatAndPay(food))
    cook.prepareFood(order)
  })

  customer.on('leaving', tip => done(null, tip))
  customer.placeOrder(menu)
}
  • Set Immediate
// In the below example the console log will not be printed. Since the subscription happened after emitting
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.emit('data', 'Hello World!');

emitter.on('data', (msg) => {
  console.log(msg);
});

// In the below example the console log will be printed. Since the setImmediate gets executed after the complete script 
// execution by when the subscription has happened
const EventEmitter = require('events');
const emitter = new EventEmitter();

setImmediate(() => {
  emitter.emit('data', 'Hello World!');
});

emitter.on('data', (msg) => {
  console.log(msg);
});

Event Loop

  • What node uses to process asynchronous actions and interface them for you so that you don't have to deal with threads

Error v/s Exception

  • Error is a problem
  • Exception is a condition
const fs = require('fs');
const path = require('path');
try {
  const = filePath = path.resolve(provess.env.HOME, '.npmrc')
  const data = fs.readFileSync(filePath);   
  // const data = fs.readFileSync(filePath, 'utf-8');
  console.log('File data is - ', data);
} catch (err) {
  if (err.code === 'ENOENT') {
    console.log('File not found);
  } else {
    throw err;
  }
}

As a Web Server

  • Save the below code in a "firstNode.js" file
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello World!');
}).listen(8080);
  • Same as above
var http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer(function (req, res) {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('Hello World!');
});

server.listen(port, hostname, () => {
  console.log('server running at http://${hostname}:${port}');
});
  • using Event Emitter: interally same
var http = require('http');

const requestListener = function (req, res) {
  res.write('Hello World!');
  // IMPORTANT :: If not the HTTP session will think that the server is still streaming data
  res.end();
};

const server = http.createServer();
server.on('request',requestListener);

server.listen(3000, () => {
  console.log('server is listening');
});
d:\> node firstNode.js
  • nodemon - install nodemon and use it same as node. This will monitor file changes and restart the server if required. Of course only for development
  • Web Frameworks - Is a wrapper around few of the node modules (ex: HTTP. HTTP/2, HTTPS...) and helps in faster development. Ex: Express JS, Koa JS, Sails JS, Meteor JS etc..
  • Templating Framework : pug js, handlebars js, ejs etc..

node & Operating System

  • 'os' module : deals with OS of the server
  • 'fs' module : deals with file system in the server
  • 'child_process' module : for running commands (.bat & .cmd ) - runs in sub process (of node process) and gives data to main process (node process)

Exports, Module, Global

  • Exporting a module
// 1.js
exports.name = "kota"
module.exports.age = 25 // same as above ( exports is an alias to module.exports )

//2.js
var data = require(./1.js);
console.log(data.name);
// 1.js
exports = [0,1,2]

//2.js
var data = require(./1.js);
console.log(data);
// 1.js
exports.getSquare = function(number) {
  return number * number;
}

//2.js
var math = require(./1.js);
console.log(math.getSquare(2));
  • global: internally used by Node | DO NOT USE
// 1.js
global.number = 3;

// 2.js
require(./1.js);
console.log(number);

Modules

  • Modules is a wrapper on set of functions. Similar to libraries. In the above example "http" is a module and by writing require('http') we will have access to HTTP module and its functionalities (like create server).

Custom Module

  • Add below code and create a file "firstmodule.js"
exports.myDateTime = function () {
  return Date();
};
  • Use it as below
var dt = require('./myfirstmodule');    -- file path
console.log(dt.myDateTime());

HTTP Module

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
  -- Add HTTP Header
  res.writeHead(200, {'Content-Type': 'text/html'});
  -- Read Query String
  res.write(req.url);
  -- Split Query String: http://localhost:8080/?year=2017&month=July
  var q = url.parse(req.url, true).query;
  var txt = q.year + " " + q.month;
  res.end(txt);
}).listen(8080);

File Server Module

  • Allows you to work with the file system on your computer (Read/Create/Update/Delete/Rename)
var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });

  -- appends content if it doesn't exist. Else an empty file is created.
  fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
    if (err) throw err;
    console.log('Saved!');
  });

  -- Deleting a file 
  fs.unlink('mynewfile2.txt', function (err) {
    if (err) throw err;
    console.log('File deleted!');
  });

}).listen(8080);

URL Module

  • Breaking the URL and reading values from it
var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2017&month=february';
var q = url.parse(adr, true);

console.log(q.host); //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(q.search); //returns '?year=2017&month=february'

var qdata = q.query; //returns an object: { year: 2017, month: 'february' }
console.log(qdata.month); //returns 'february'
var http = require('http');
var url = require('url');
var fs = require('fs');

http.createServer(function (req, res) {
  var q = url.parse(req.url, true);
  var filename = "." + q.pathname;
  fs.readFile(filename, function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    } 
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080);

Events Module

var events = require('events');
var eventEmitter = new events.EventEmitter();

//Create an event handler:
var myEventHandler = function () {
  console.log('I hear a scream!');
}

//Assign the event handler to an event:
eventEmitter.on('scream', myEventHandler);

//Fire the 'scream' event:
eventEmitter.emit('scream');

Upload Files Module

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;
      var newpath = 'C:/Users/Your Name/' + files.filetoupload.name;
      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.end();
      });
 });
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);

Email Module

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com, myotherfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
  -- for html ===> html: '<h1>Welcome</h1><p>That was easy!</p>'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

NPM - A package manager for Node.js packages (modules) & also a command line application

// command prompt
npm install upper-case

-- usage
var uc = require('upper-case');
  • Dependency tree & updates
> npm outdated 
// will list the dependency installed versions, requested versions (if any change because of semver) & latest versions of the library
  • publishing to npm registry
> npm login
> npm publish

Modularizing Node.js application

  • By using exports in different JS files
// cook.js
const ingredients = 'stuff'
const prepareFood = (order, done) => {
  // prepare food
}
module.exports = { prepareFood }

// customer.js
class Customer {
  // methods and properties
}
module.exports = Customer

// waitress.js
const cook = require('./cook')
const Customer = require('./customer')
//cook.prepareFood()
// new Customer()

Testing

  • OPEN SOURCE TOOLS :: MochaJS (BDD), Chai (Assertion Library), Sinon (Spies, Stubs and Mocks), Istanbul (Code Coverage)

NPM Scripts

  • Scripts (/commands) which helps in automating
// pacakge.json

"scripts:" {
  "start": "node server.js",
  "test": "jest",
  "check": "eslint server.js"
}

// in CMD
> npm run start OR npm start
> npx test (npx = npm execute -> Will find the binary under node_modules)
> npm run check

// for more events
> npm help npm-scripts
output => Ex: prepublish, prestest, posttest etc..

COMMENTS

  • Node.js can be used to connect to various Databases like MySQL, MongoDB, MSSQL, etc..
  • Node.js can be used to connect to Raspberry Pi (mini computer)
  • 6.10.3 Built in Modules
  • Streams:: Helps is doing things in chunks
  • "process" object helps node to interact with OS
  • Node Clusters : Ensure that each node process is running for each CPU cores.
    • Even if it is single Core, run a cluster so that the cluster will monitor the process & start a new process if it crashes. Ex: PM2 tool helps in running cluster nodes

Not Fit

  • CPU intensive tasks: Designed to build scalable network (i/o) applications (node shouldn't be used where it spends too much time on its own)
  • Javascript :: Not Type Based (Typescript solves it)

Clients

  • webpack, gulp, eslint, yeoman etc...
  • Desktop Applications :: ElectronJS Framework - Ex: Skype, Slack, Github Desktop, Visual Studio Code, Hyper, Atom

Clone this wiki locally