You Don't Know Node - ForwardJS San Francisco

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right how you guys doing ready for some party all right let's get disappointed first maybe so he who's here you're either in this room to learn a little bit of note or to be pissed off but what you're gonna see right so who's here to learn note all right perfect so the talk title is you don't know node and if you don't really like the fact title blame Dave because he picked it in reality the more correct term is you probably don't know most of note and I actually stalled this talk title from my good friend as a modern and if you don't know as uh if you don't follow as that you should definitely follow him and follow all his good work on node here's my beef most educational content about node teaches node packages and they're probably nothing wrong with that because you probably need to use node packages but those node packages actually wrap the node runtime and when you run into problems you're most likely running into problems in the stack in the node runtime itself so if you don't know the node runtime you're in trouble so let's play a game I'm gonna put questions on slides and you try to answer them in your head and I'm gonna attempt to answer them after that I need you just validate your node knowledge so let's try this one what is the call stack and is it part of the eight who can confidently answer this question really all right so the call stack is definitely part of v8 and the call stack is the structure that v8 uses to keep track of function invocations so every time you the function VA places that on the call stack and keeps doing so including functions that you invoke recursively and once the invocation is complete v8 is going to pop one function at a time and execute it so it's very important to understand this in node because you only get one call stack so if you keep this call stack busy your node application is busy so you want to keep track of what goes in the call stack how about this one what about the event loop and is that part of v8 the more visual question here is if I give you this diagram which basically represent every node application from beginning to finish where is the vet loop leave that loop is wanna shout out an answer nope libuv the event loop is in this live UV library which is an abstraction just to provide the event loop among few other things but it's totally not part of v8 what is they meant loop let's attempt to define the event loop so one definition says it's the entity that handles external events and converts them into callback invocations maybe not very clear probably not let's try another one it's a loop that picks events from the event queue and pushes their callbacks to the call stack maybe a little bit more clear but still not really and there's a reason it's really hard to define the event loop or to just understand that event loop from an abstract definition because the event loop is just one part one tiny part of a really big picture and you actually need to understand this picture to understand the event loop you need to understand that there is a v8 engine you need to understand that there are node API is in the play and you need to understand that are there are some event queues and the node API are things like set timeout you know that set timeout is not JavaScript right if I grip the source code of v8 set timeout does not exist it's not JavaScript it's a node API and in the browser it's a browser API so things like that are in the API land the event loop sits in the middle of this picture and it's like the organizer the one who decides what goes where and it's really simple so when there are events in they thank you and there are nothing in the stack the event loop just pushes something to the stack that's it it's just a giant loop that does that all right let's write one let's try one more question what will know do when both the call stack and that is that loop hue or empty it will simply exit so when you start a program no it automatically starts the event loop for you when the event loop is done there's nothing for the event loop to do node will exit so to keep node 2 running you need to place something in the event queue so when you start an HTTP server or when you start a timer you actually put something in a thank you that says keep running here keep listening here and keep the program running all right let's write one more question besides v8 and libuv what other external dependency does node have these guys HTTP parser sierra's open SSL and the Lib all of these are external to node they have their own source code they have their own license and notjust uses them so you want to keep track of that you want to know where is your program running if you're using Li lip you're using some other library that's not node and you might run into troubles there what about this one can node work without v8 it says yes yes it can you can run node with chakra engine and I hear it's really good I haven't really tested it but here it's actually better than v8 kudos Microsoft let's get into more detailed questions right I know it's end of date but so you can when you export the API if you module you can do module exports G equal you can also do exports G equal you can do module the exports equal something you cannot do experts equal something why that doesn't make sense because exports is just a reference to module that experts and when you change exports you change that reference and you're no longer changing the API you're changing some local variable cool so let's keep on this topic if you have module one that defines a global variable G and in module two you require that module one and you try to access G you get G is undefined why if you do that in a browser you definitely can access G all the scripts can access any variable defined in any script why is it not the case in node who knows the answer because every node file gets its own if automatically behind the scenes in fact if you drop into a node script and you just console.log argument just one line console.log arguments and you run that what are you gonna get you are gonna get some actual arguments why because that's a function what you executed is a function because node wrapped your code in a function so you'll get those arguments those five arguments one more related question the exports require and module are all globally available in every node file but they are different it's only in X when you expect a require object in one node file it's different than the other but it's globally available just do require or module how because of that magic a fee that magic if he is actually exactly that it's a function that passes exports require module file name and their name and those five magic objects appear to be global in your node file but they're not they're just function arguments so this require thing is really advanced in fact there are five major steps that happen every time you require a node file what are they you can think of those as maybe node interview questions maybe not but I would ask you that in an interview question the five major steps are when you require something node will attempt to resolve what you require first stage it will try to map that string that you required into an actual path on the file system this path could be local to your node it could be under your node modules it could be under the node modules if you're a parent and many other paths so that's the first step Matt a string into a path then node loads the content of that file after it resolves it into memory and after that it wraps the content with that magic avi the function and after that it evaluates the file probably with v8 and once it's done evaluating the file it will cache it which means the next time you require the exact same file those steps are not gonna happen it will just read it from the cache related how can you check or the existence of a local module I don't want to run it I just want to see if it exists you can do that with the require dot resolve which means just resolve it don't load it don't evaluate it don't cache it just resolve it if that's successful the module exists if it's not it will throw an error that you can catch all right how about a harder question this does were the easy ones what our circle or modular dependencies in node well simple if you have module ones that require module 2 and module 2 that requires module 1 what's gonna happen who says you'll get an error you will not get an error no it actually allows that and when module 1 require module 2 since module 2 needs module 1 and module 1 is not done yet module 1 will just get a partial version of module 2 and that leads to really cryptic scenarios that you are gonna face for sure if you do that so just be aware of that all right how about an easy question what are the three extensions that will be automatically tried by the required function we know that G is right but the required function will also try JSON and that node so if you have a JSON file you should just require it directly and nobody will actually take that JSON parse it and put it in a JavaScript object for you you can also require that node file which is a binary file so if you have some C code you can just compile that C code into a duck node file using some node package and you can just require the C code directly you don't have to require the Java Script and that's a lot for low-level capabilities for you so there's so much information about requiring modules in node and actually this is your first step to just understand this require concept and I wrote about it in length all right let's get into a different topic what are the major differences between spawn and exec you might ask what is spawn and exec so you have node and you have the operating system and you can have node run a command in the operating system and have the operating system give node the output of that command easy you can do that in all languages really but the question is spawn and exact both allow you to do that what is the difference and how significant is the difference is you want to pick one of those and the difference is spawn by default does not use a shelf while exec does more importantly it spawn works with streams while exec buffers the whole output for you so from those simple differences which one is better F spawn is much better for both reasons because you don't want to use a shell if you don't want if you need don't need to because using a shell is actually a security risk you might be vulnerable to shell injection so if you don't need a shell don't use it and if you have a command that outputs a really big amount of data for you you want to do that through streams you don't want to do that through a buffer you don't put the whole output of the command in memory that's just a waste of memory if you have if you're getting the output of the command and you're doing something with it do you want to do it through streams period so you have a few other options there's for an exact file for and exact and fork is an interesting one we're gonna cover some questions about Fork as well and I wrote about that too all right let's try easier questions how can we make note always use JavaScript strict mode so I don't want to do use strict everywhere I don't want to track that I just want to strict mode all over the board it's as simple as just doing that new node - - use strict and this particular argument is interesting because it's a v8 argument it's not a node argument no just relays that - v8 and the eight will start in script mode and there are probably a hundred more arguments just like that toggle that you can toggle all the settings actually with modules in node it's good question what is process Arg V and what type of data does it hold tell me you know that you should know that process are V when you specify arguments to the node command those arguments get captured in an array and all the argument values are captured as strings so if you have numbers in those arguments those numbers will be captured as strings as well all right let's try a little bit more challenging question how can we do one final operation before a node process crashes and can that operation be done asynchronously the answer is you can use the uncaught exception on the process object so this uncaught exception is like a generic error I'm crashing what do you want to do so you can do synchronous operation here you cannot do asynchronous operations why because the event loop just gave up at this point and says I'm not working anymore go crash so you cannot go back to the event loop you can do synchronous operations you can write to files synchronously the more important point about uncaught exception who used uncaught exception before the more important point about uncaught exception is that you have to let the process exit you should not omit that process exit lines that's not optional you have to let the process exit because basically at this point when you're inside an uncaught exception your application state is unpredictable something is terrible with your code and your better option is to just reset do not assume that you can continue running any and the thing is if you register a handler for the uncaught exception and you don't do process the exit node will not crash it will stay running in that unpredicted State so don't do that let's go with easier questions what are the built-in dot commands that you can use inside nodes rebel when you just run node you get a rebel what are the dot commands and easy and you can see those with just dot help and some of those are really helpful like that load that save and more recently the editor gives you an actual editor inside your nodes rebel alright let's try an easier question and I hope some of you know this because it's really useful what does the underscore mean inside nodes rebel anybody used it before it means last executed value so if I run math dot random I get a random value and if I want to capture that random value in a variable of course I can't do math dot random again note automatically captures that in the underscore variable and I can just use that very useful right all right a little bit more challenging what's the difference between buffer allocate and buffer allocate unfaith sounds scary huh because there is buffer allocate gives you a chunk of the memory and actually reset that chunks for you with zeros buffer allocate unsafe just give you a chunk of the memory and you can read what's already in that memory so you can actually leak information with using allocate unsafe but it's actually faster then allocate because it doesn't do that reset about the cluster module how does the cluster module work in node so the cluster module is to help us with scalability and help us particularly with the cloning scalability strategy not the other ones there's more scalability strategies the cluster module is just for the cloning scalability strategy and the idea is really simple you have a master process inside one server and that master process Forks multiple workers and the requests come to your master process and that master process determines where to send that request to which work by default no does just round-robin algorithm here and this is very helpful if you're scaling in one server that has multiple CPU cores you don't need any tools you can just use the node cluster module few lines of code you get that but that comes with a price of course any scalability comes with a price for example these workers have their own memory their own v8 their own call stack so you can no longer share things in memory you can no longer like cache things in memory if you want to cache things in memory you have to use a different entity with its own API and have all the workers work with that entity so if you're using the cluster module you want to be aware of that here's the thing many tools out there for example if you're using PM 2 uses the cluster module by default right so if you're using PM 2 in a multi-core CPU machine and you cache things in memory you're just simply doing it wrong because some worker will not be aware of the other workers memory I wrote about that - been busy alright when is it okay to use the file system synchronous methods every file system has a synchronous method so you've got right file you have right file think why would you do that sometimes it's okay for example in your initializing step if you're just loading the server it's sometimes okay to just do things synchronously because everything else depends probably depend on that data that you're doing it synchronously that's okay it's a one-time thing but if you're doing a synchronous method inside a handler like for example inside the create server that's 100% wrong you should not be doing that because that synchronous method will basically block the event loop and nobody else can do any hang on the server so just be aware of that all right let's try easier questions how can you print only one level of a deeply nested object that's actually a JavaScript question not node specific but it's really helpful if you're printing an object that has like multiple levels that big nest nested a tree and you're just interested in the first level you can use the console d'oeuvre method and just specify depth zero how cool is that it gives you just the first level and all the other levels will just be stubs this is a function this is an object cool all right this one next one is one of my favorite how can you debug a node program in chrome dev tools you can't it's as simple as that in recent nodes just did node - - inspect and node will give you a URL that you can run inside chrome and it's connected to the node process and you can use all the things the good things in chrome dev tools to debug your node application I don't know about this all right you guys are good let's talk about asynchronous so we know that originally node is all about callbacks and we know now javascript has promises which are cool and more as of recent there's also async await to handle promises which is cooler here's my question about callbacks can callbacks be synchronous and the answer is yes of course callbacks do not indicate a synchrony just because it's a callback does not mean it's asynchronous code you can totally do a synchronous callback and sometime your function is going to be either synchronous or asynchronous based on something ditions and that's actually bad you want your functions to be either always synchronous or always asynchronous all right what about event emitters what's the difference between using event emitters and using callback functions for asynchronous handling of code what are event emitters well event emitters are just objects you require events you get an event emitter you can extend that in the limiter if you want your own custom logic but then event emitters can just omit those strings dynamic strings any string and you can register handlers to be invoked one when those strings get emitted very simple what's the major difference why can't we just use callbacks callbacks are like a special form of event emitters that you can register to once so if you want code if you want multiple callbacks to be fired for the same event you want to use an event emitter because if you have multiple things to be done it's a lot cleaner to have them in separate functions if you want to do the same with callbacks you have to drop into that single callback that you get and start doing if statements and stuff so event emitters and event emitters are actually super important because they're at the core of everything else in node mostly all other objects in nodes implement event emitters so they're very important I wrote about that too what's an example with a built-in stream in node that is both readable and writable maybe an earlier question is what is stream terminal socket a new socket yeah totally but some be more interesting readable and writeable streams or transform streams like create cipher and create gzip because they work with streams and you get to do something on streams while you're reading and writing them so that's interesting streams are the next very important topic in note because here's the thing here's my global bold statement if you're not using streams you're doing it wrong what's the difference between past and flowing modes of readable streams readable streams can either be paused in which case which is the default in which case you have to pull information out of the string you have to say stream I want to read from you or they can be flowing and in that case they're actually running so you have to register events to capture their data so by default you get paused and you can actually switch back and forth between pauses and flowing and sometimes that happens automatically by node so when you register a listener nodes automatically switches a paused stream into a flowing stream and when you remove that listener it switches it back totally different modes of streams I said streams are super important because they're all over the place everything in node is a freaking stream HTTP responses requests file system reads rewrites Z lib crypto TCP child processes standard input and output they're all streams and that's a really good thing because it allows you to not use memory when you don't need to working with big data doesn't require allocating a lot of memory node you just use strings and of course I wrote about that too but let me attempt to explain streams that's how important streams are when I attempt to explain it to you right now what are streams streams are collections of data that might not be available at all all at once and don't have to fit in memory so think of them as just race but there's special types of a race they're a race that you don't have you get one piece at a time so you don't fit them all in memory and that's a good thing because if the array has a million element you don't want to deal with the million element all at once get some elements deal with them write them that's streams so for example we said examples of streams there's readable writable duplex and transform and some examples readable stream you can do FS create read stream you get a readable stream out of file FS create write stream you get a writable stream for a file duplex sockets are duplex strings which means you can read and write from them with streams and transform streams is the G's there's the zip library for example Cielo bribery library so if you have a file that's a 10 gigabyte and you want to zip that file it cannot actually read the file to memory no it will just not allow you to do that because the default buffer size is not that big you have to use streams which means you get one chunk of that file zip it write it to the zip file but you're not done you get more chunks do that write it and you can actually zip this 10 gigabyte file by using maybe like 40 megabyte of notes memory using streams that's how cool streams are there's one thing that I want you to remember all streams are event emitters so you can register handlers on those streams and work with those again event emitters super important the global strings are hard the global way to use streams is that pipe you get a readable stream and you pipe it into a writable strings that's it just remember this slide because it's super important the most important by today readable the pipe unraidable you can think of them actually as Linux pipe commands you have multiple commands and Linux and you pipe one on the other and the other only another one and you get more powerful programs by doing so in Linux the exact same philosophy you get one node stream that is readable you pipe it on the writable stream and if that writable stream is also readable you can chain and pipe it on another transform stream in this picture B and C have to be both readable and writable because we're chaining them a can be just readable and D can be just writable but the others are have to be transform streams or duplicate streams at least and that's equivalent to that just aid the pipes B and then B the pipe C and so on alright so when we talk about streams we actually talk about two very different things we can be talking about implementing streams but we can also be talking about consuming streams and those are actually totally different so if you're implementing streams you require that stream library the built-in stream library that's when you need to require the built-in stream library is when you implement the stream but in most cases you just use streams you just consume streams because they're all over the place so if you're inside an express HTTP server that R is the res object that's a string and you should know that it's a stream because now you can stream information to your clients but you're only consuming that stream that's why you didn't need to require streams cool all right so I'm doing this in more details on Friday 7 hours of this relief but that's all about I have for you thank you [Applause]
Info
Channel: ForwardJS
Views: 22,431
Rating: undefined out of 5
Keywords: ForwardJS, Forward, JavaScript, San Francisco, Conference, Tutorial, Free Resources, Leaning, JS, 2017, Training, Education, Best Practices, Tips, Tricks, Samer Buna, Node.js, Azat Mardan, runtime, new features, keynote
Id: oPo4EQmkjvY
Channel Id: undefined
Length: 35min 25sec (2125 seconds)
Published: Wed Sep 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.