Skip to main content

Posts

Showing posts from October, 2019

Scaling Node.JS

Three areas to take care in scaling Multiple instances Microservices Database Partitioning How to fork? const { fork } = require('child-process); const processes = [   fork('./app', ['3001']),   fork('./app' , ['3002'] ),   fork('./app' , ['3003'] ) ] Check CPUs on machine const cpus = require('os').cpus; console.log(cpus); Run as many instances of node app as many CPUs are available. Write the following to index.js and run in Node const   http  =  require ( 'http' ); const   cluster  =  require ( 'cluster' ); const   cpus_len  =  require ( 'os' ). cpus (). length ; if  ( cluster . isMaster ) {      console . log ( "FORKING" ,  process . pid );      for  ( let   i  =  0 ;  i  <  cpus_len ;  i ++) {          cluster . fork ();   ...

Where to use Number() and parseInt() in Javascript?

Where to use Number() and parseInt() in Javascript? Number(object) tries to convert the entire string to a number(which can also be a float) while parseInt(object) parses up to first non-digit and returns whatever value has been parsed. So, if you have "1234@gmail.com" and you want to get 1234 out of it, then use parseInt("1234@gmail.com"); But, if you have "1234" and you want to convert the entire string as one number then use Number("1234");

NodeJS Arguments Passed While Running File

Use process.argv to access arguments passed while running a node.js application. console . log ( process . argv ); Run the file node test drive --speed=30 --model=nissan Output [ 'C:\\Program Files\\nodejs\\node.exe', 'C:\\Users\\Dell\\Desktop\\Angular\\myapp\\test011args', 'drive', '--speed=30', '--model=nissan' ] Doing the same using yargs npm package Install Yargs npm install yargs Write the file const   yargs  =  require ( 'yargs' ); console . log ( yargs . argv ); Run the file node test drive --speed=30 --model=nissan Output { _: [ 'drive' ], speed: 30, model: 'nissan', '$0': 'test011args' } Uargs converts the arguments in an object

Synchronous and Asynchronous File Delete, Read, Write, Append in NodeJS

Synchronous and Asynchronous File Delete in NodeJS Synchronous Delete File const   fs  =  require ( 'fs' ); // Unlink is synchronous while unlinkSync is asynchronous. // Unlink returns error or nothing. If nothing then task is successful. If error, task is unsuccessful. fs . unlink ( './test5.txt' ,  err   =>  {      if  ( err ) {          console . log ( err );     }  else  {          console . log ( "Done synchronously." );     } }); console . log ( "Independent line" ); Asynchronous Delete File const   fs  =  require ( 'fs' ); try  {      fs . unlinkSync ( './test5.txt' ); }  catch  ( ...

Creating Our Own Node Package

Create a package named useless in terminal using: npm init Add your function file const useless = () => 'you wasted time here!' module.exports = useless; Add a file named index.js const useless = require('./useless'); module.exports = useless; Add the entire package to github. USE OUR PACKAGE Open a new project Import our package using the github link and running the following in terminal: npm install VinitK/a-useless-package#master Create a file named app.js and import the package we created and use it. const useless = require('a-useless-package'); const _ = require('lodash'); console.log(useless()); const truthyArray = _.compact([1, 0, false, '', null, undefined, true, 2, -1, 'true', [], [1, 0], {}, { 'a': 1 }]); console.log(truthyArray); That's it!

Two ways of writing modules in NodeJS

Two ways of writing modules Using module.exports const add = (n1, n2) => n1 + n2; const sub = (n1, n2) => n1 - n2; const mul = (n1, n2) => n1 * n2; const div = (n1, n2) => n1 / n2; module.exports = { add, sub, mul, div }; Using exports - when there are more than one functions/variables to be exported exports.add = (n1, n2) => n1 + n2; exports.sub = (n1, n2) => n1 - n2; exports.mul = (n1, n2) => n1 * n2; exports.div = (n1, n2) => n1 / n2; How to use these modules? Importing entire file const test = require('./test'); console.log("add: ", test.add(2, 1)); console.log("sub: ", test.sub(2, 1)); console.log("mul: ", test.mul(2, 1)); console.log("div: ", test.div(2, 1)); Importing functions/variables that are to be used const { add, sub } = require('./test'); console.log("add: ", add(2, 1)); console.log("sub: ", sub(2, 1)); That is it!

Three Ways To Exit From Node REPL

Install Node Open Command Line/Terminal To check the running node version run the following in Command Line/Terminal: node -v To start node REPL run the following in Command Line/Terminal node Three Ways To Exit From Node REPL Ctrl + c Ctrl + c .exit process.exit() That is it!