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
//we could avoid buffering data altogether and only generate the data when the consumer asks for it. | |
//We can push chunks on-demand by defining a ._read function | |
//To show that our _read function is only being called when the consumer requests, | |
//we can modify our readable stream code slightly to add a delay: | |
var Readable = require('stream').Readable; | |
var rs = Readable(); | |
var c = 97 - 1; |
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 Readable = require('stream').Readable; | |
var rs = new Readable; | |
rs.push('beep'); | |
rs.push('boop\n'); | |
rs.push(null); //Tells the consumer that rs is done outputting data | |
rs.pipe(process.stdout); |
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 http = require('http'); | |
const fs = require('fs'); | |
var server = http.createServer((req, res)=>{ | |
var stream = fs.createReadStream(__dirname + '/data.txt'); | |
stream.pipe(res); | |
}); | |
server.listen(3000); |
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 http = require('http'); | |
const fs = require('fs'); | |
var server = http.createServer((req,res)=>{ | |
fs.readFile(__dirname + '/data.txt', function(err, data){ | |
if(err){ | |
res.end(err); | |
} | |
res.end(data); | |
} |
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 obj ={ | |
prop: (() => { return ( 3 > 4 ? 'yes' : 'no' );})(); | |
} | |
console.log(obj.prop); //output: no |
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
//We are triggering start but we are catching done | |
var Job = require('./nodejs_inherit_events_main_job'); //inherit from main Job | |
var job = new Job(); | |
job.on('done', function(details){ //Subscribe to the main job's done event | |
console.log('Job was completed at', details.completedOn); | |
job.removeAllListeners(); | |
}); | |
job.emit('start'); //Fire-up the main job |
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
//We emit done and catch start events | |
var util = require('util'); | |
var Job = function Job(){ | |
//we will use it later to pass this to a closure | |
var job = this; | |
job.process = function(){ | |
//emulate the delay of a job async | |
setTimeout(function(){ |
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 events = require('events'); | |
var emitter = new events.EventEmitter(); | |
emitter.once('knock', function(){ //subscribe to the event | |
console.log('Who\'s there???'); | |
}); | |
emitter.on('knock', function(){ //subscribe to the event | |
console.log('Leave us in peace, please!!!'); | |
}); |
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
console.log('Step: 1'); | |
var startTime = Date.now(); | |
console.log('Start time: ', startTime); | |
for(let i=1;i<10000000000;i++){ | |
//Do Nothing | |
} | |
var endTime = Date.now(); | |
console.log('Step: 2 started!'); | |
console.log('End Time: ', endTime); |
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
require('http').createServer((req,res)=>res.end("Hey!")).listen(3000,'127.0.0.1'); |