Node - JS: Mendel Rosenblum
Node - JS: Mendel Rosenblum
js
Mendel Rosenblum
Implementation: Implementation:
Thread switching (i.e. blocking) and a Event queue processing
scheduler
Implementation:
Implementation:
Event queue processing
Thread switching (i.e. blocking) and a
scheduler
var notGlobal;
function func1() {}
function func2() {}
module.exports = {func1: func1, func2: func2};
CS142 Lecture Notes - Node.js
Node modules
● Many standard Node modules
○ File system, process access, networking, timers, devices, crypto, etc.
● We use:
○ Express - Fast, unopinionated, minimalist web framework (speak HTTP)
○ Mongoose - Elegant mongodb object modeling (speak to the database)
● Node add a Buffer class - Optimized for storing and operating on binary data
○ Interface looks an array of bytes (like the OS system calls use)
○ Memory is allocated outside of the V8 heap
● Mental model
If code doesn't block: Same as thread programming
If code does block (or needs to block): Need to setup callback
● On emit call listeners are called synchronously and in the order the listeners
were registered
readableStreamEvent.on('end', function() {
// Called after all chunks read
console.log('got all the data');
});
writableStreamEvent.on('finish', function () {
console.log('file has been written!');
});
writableStreamEvent.write('Hello world!\n');
writableStreamEvent.end();
net.createServer(processTCPconnection).listen(4000);
● Node.js
If we want to use fileContents how do we know when all reads are finished?
Recall: Can't wait in NodeJS
● Yuck!