|
| 1 | +# Node.js |
| 2 | + |
| 3 | +A computer program can run in many host environments. |
| 4 | + |
| 5 | +Programs run on desktops, tablets, small devices, smart devices. |
| 6 | + |
| 7 | +Some programs run across various operating systems; this is useful. |
| 8 | + |
| 9 | +This is possible because the language of your program has interpretters and compilers that work in a particular host environment. |
| 10 | + |
| 11 | +It's very environmental. |
| 12 | + |
| 13 | +Javascript programs run inside every browser tab. |
| 14 | + |
| 15 | +On billions of devices, across all operating systems. |
| 16 | + |
| 17 | +Node.js created an environment for running javascript outside of the browser as well. |
| 18 | + |
| 19 | +This allowed for many new kinds of applications to be written in Javascript, which previously worked only in the browser. |
| 20 | + |
| 21 | +Node.js was a very clever hack, and has become a invaluable tool for web developers. |
| 22 | + |
| 23 | +Node.js performs very well for what it was intended. Node.js was intended for writing web server applications, the back-end. |
| 24 | + |
| 25 | +Node.js also performs well enough for many things it was never intended, because it made writing myriad various programs much less painful. |
| 26 | + |
| 27 | +This is entirely due to a large commnity of open source programmers, slowly chiseling at good forms, styles, and practices. |
| 28 | + |
| 29 | +Many of these would be codified into Node.js, and from there influence code writing for front-end apps. |
| 30 | + |
| 31 | +And there is no doubt Node.js has set the bar for community backed open source development, as much as it sets a bar for high performance web apps. |
| 32 | + |
| 33 | +Perhaps best of all, Node.js allows the intrepid hacker to create full stack, web applications with a single language: javascript. |
| 34 | + |
| 35 | +# Events |
| 36 | + |
| 37 | +EVENTS are an important concept for the javascript and Node.js programmer, as well as designers of interfaces, or machines with sesnors. |
| 38 | + |
| 39 | +What is an event? Unlike many programming concepts, an event is exactly what you should expect. |
| 40 | + |
| 41 | +An event is when something happens. |
| 42 | + |
| 43 | +Javascript is the language of the browser, and that includes user interactions--events. |
| 44 | + |
| 45 | +Therefor Javascript has always been event oriented. |
| 46 | + |
| 47 | +Node.js took this evented straight to the core, creating an event driven architecture, which turned out to be a brilliant idea. |
| 48 | + |
| 49 | +Almost everything is an event. |
| 50 | + |
| 51 | +A click, a key press, a scroll motion, are events. |
| 52 | + |
| 53 | +A connection, an update, an interval, are events. |
| 54 | + |
| 55 | +Some events can be broken down into several events. |
| 56 | + |
| 57 | +A button is pressed, is held, is released. |
| 58 | + |
| 59 | +A conncection is opened, is upgraded, is closed. |
| 60 | + |
| 61 | +Many programmers failed to realize this nature of javasctipt, because they wrote most of their code in another language, which did not emphesize events. |
| 62 | + |
| 63 | +The result are many confusing paradigms for wriring javasctipt programs, such as Model-View-Controller, or MVC. |
| 64 | + |
| 65 | +With Node.js, events are fundamental, the basis for all higher order constructs, most importantly STREAMS. |
| 66 | + |
| 67 | +A stream takes a pattern of events, and creates a flow out them. |
| 68 | + |
| 69 | +But how do you write a program that anticipates various events, and then does something with the potentially unknown consequences of these events, such as an error, or the receipt of arbitrary data? |
| 70 | + |
| 71 | +There is only one answer. |
| 72 | + |
| 73 | +You must write functions that listen for verious, specified events, and you must write functions that handle the result of the events, if and when they occur. |
| 74 | + |
| 75 | +The first kind is a "listener". |
| 76 | + |
| 77 | +The second is a CALLBACK. |
| 78 | + |
| 79 | +# Callbacks |
| 80 | + |
| 81 | +A CALLBACK is simply a function, no different than any other function, which is called upon an event. |
| 82 | + |
| 83 | +Most confusion about calbacks stems from confusion, or laziness, about javascipt functional syntax. |
| 84 | + |
| 85 | +Consider the following: |
| 86 | + |
| 87 | +```js |
| 88 | +event.on('update', function(data){ |
| 89 | + console.log(data) |
| 90 | +}) |
| 91 | +``` |
| 92 | +To the uninitiated, this may look like a pinata of inscruotable syntax. |
| 93 | + |
| 94 | +A function is being called, which takes two arguments. One argument is the string 'update', the next argument is a function. This is confusing because that function is written out within the parenthesis of the function being called. |
| 95 | + |
| 96 | +Another way looks like this: |
| 97 | +```js |
| 98 | +var callback_Function = function(data){ |
| 99 | + console.log(data) |
| 100 | +} |
| 101 | + |
| 102 | +event.on('update', callback_Function) |
| 103 | +``` |
| 104 | + |
| 105 | +Now you can see that the callback is a generic function, and event.on is a function being called() with two arguments, one of them a function. |
| 106 | + |
| 107 | +When an event fires that is an 'update', call the supplied function, in this case 'callback_Function'. |
| 108 | + |
| 109 | +It is very common in javascript to supply a function as an argument, like in real life you might give directions "in the event" something occurs. |
| 110 | + |
| 111 | +Node.js is build around events. This is because there is often some time between the initiation of a sequence, and it the events which are produced. |
| 112 | + |
| 113 | +Direct your program to open a file, and when the file is open, do something. |
| 114 | + |
| 115 | +so much for events and callbacks. |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
0 commit comments