Skip to content

Instantly share code, notes, and snippets.

View devarajchidambaram's full-sized avatar

devaraj devarajchidambaram

View GitHub Profile
@devarajchidambaram
devarajchidambaram / spawn vs child_process
Created August 28, 2018 13:32
Difference between spawn and exec functions of child_process
Difference between spawn and exec functions of child_process
The Node.js Child Processes module (child_process) has two functions spawn and exec, using which we can start a child process to execute other programs on the system. Those new to child_process may wonder why there are two functions to do the same thing, and which one they should use. I'll explain the differences between spawn and exec to help you decide when to use what.
The most significant difference between child_process.spawn and child_process.exec is in what they return - spawn returns a stream and exec returns a buffer.
child_process.spawn returns an object with stdout and stderr streams. You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have. spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc.
child_process.spawn is "asynchronously asynchr
@devarajchidambaram
devarajchidambaram / promises.js
Last active August 28, 2018 10:52
multiple then in the promises usage
/*
USE OF MUTIPLE THEN IN THE PROMISES
Actually in then we can return a sync value or
multiple async values
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining
**/
var p1 = new Promise(function (resolve, reject) {
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const session = require('express-session');
const cors = require('cors');
const mongoose = require('mongoose');
const errorHandler = require('errorhandler');
//Configure mongoose's promise to global promise
mongoose.promise = global.Promise;
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const session = require('express-session');
const cors = require('cors');
const mongoose = require('mongoose');
const errorHandler = require('errorhandler');
//Configure mongoose's promise to global promise
mongoose.promise = global.Promise;
@devarajchidambaram
devarajchidambaram / prependerListener.js
Created August 2, 2018 13:34
How to set a callback function to be the first callback in EventEmitter?
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
myEmitter.on('fire', ()=>console.log('fire1'))
myEmitter.on('fire', ()=>console.log('fire2'))
myEmitter.on('fire', ()=>console.log('fire3'))
myEmitter.prependListener('fire', ()=>console.log('fire tooppppp'))
myEmitter.emit('fire')
@devarajchidambaram
devarajchidambaram / get_version.js
Created July 24, 2018 11:43
get npm module version name
var path = require('path')
var fs = require('fs');
function getNpmVersion(npmName) {
try {
var npmPath = require.resolve(npmName)
var npmDirName = path.dirname(npmPath)
return JSON.parse(fs.readFileSync(path.join(npmDirName, '/package.json'))).version;
} catch (e) {
@devarajchidambaram
devarajchidambaram / v8profiler.js
Created July 13, 2018 10:38
how to profile nodejs applications
var fs = require('fs');
var profiler = require('@risingstack/v8-profiler')
var request = require('request')
var samplingIntervalMicros = 1000;
var express = require('express')
var app = express();
var bodyParser = require('body-parser')
var enableProfiler = false;
@devarajchidambaram
devarajchidambaram / environment.txt
Created July 13, 2018 05:54
SET environment variable in NODEJS
NODE_ENV works like any other environment variable (e.g. PATH) and it depends on your platform how to set it:
Linux and OSX: export NODE_ENV=production
Windows: SET NODE_ENV=production
In nodejs app we can get it
process.env.NODE_ENV
@devarajchidambaram
devarajchidambaram / wrap_express_middlewares.js
Created July 10, 2018 09:48
Wrap express js middlewares
//Referred link https://www.lunchbadger.com/tracking-the-performance-of-express-js-routes-and-middleware/
var express = require('express')
var uuid = require('uuid')
const bodyParser = require('body-parser');
const {
EventEmitter
} = require('events');
const profiles = new EventEmitter();
@devarajchidambaram
devarajchidambaram / timer.js
Created July 5, 2018 09:46
Pass argument setTimeout callback (fn, time, arg)
function myFunc(arg) {
console.log(`arg was => ${arg}`);
}
//In nodejs we can pass the arguments..
// nodejs, actually provides its own implementation of these timer methods
setTimeout(myFunc, 1500, 'funky');
//> clear
//clearImmediate clearInterval clearTimeout