This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |