edit-article
Home
Up
Delete
Article Name:
Article Description:
VIDEO-TLDW-learn-node-js-in-one-hour[78:00] by Mosh H. @ programmingwithmosh.com - Mosh goes over some of the node development fundamentals including installation, npm, using npm, working with native node modules , creating your own modules and more,
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">learn node.js in one hour</h1> <h2>[WHAT]</h2> <ol> <li>] by Mosh H @ __ - </li> </ol> <h2>[WHY]</h2> <ol> <li>] </li> </ol> <h2>[WHERE]</h2> <ol> <li><strong>] WATCH THE FULL VIDEO<br /></strong></li> <ol> <li>]</li> </ol></ol> <h2>[WHEN]</h2> <ol> <li>] 2019-11-22</li> </ol> <h2>[EXAMPLE]</h2> <ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=0s" target="_blank">[00:00</a>] What is Node</strong></li> <ol> <li>[] node is an open source, cross platform runtime environment for executing javascript code outside of the web browser</li> <li>[] node is used for rapidly prototyping and agile development</li> <li>[] node is used for building backend services like apis that power client applications, like web and mobile clients</li> <li>[] <strong>fast</strong> -</li> <ol> <li>[] <span style="background-color: #c0c0c0;">[] node uses a non blocking <br /></span></li> <li><span style="background-color: #c0c0c0;">[] asynchronous architecture by default</span></li> </ol> <li>[] <strong>scaleable</strong> -</li> <ol> <li><span style="background-color: #c0c0c0;">[] node is built using an event driven architecture, </span></li> </ol> <li><strong>[x] adoption</strong> - used by large companies like Paypal, walmart</li> <li>[] <strong>study</strong> by paypal found that company developers were able to build app twice as fast with fewer people, <strong>33% less lines</strong> of code, <strong>40% fewer files</strong>, <strong>2x requests per sec</strong> and <strong>35% faster response times</strong></li> <li>[] use <strong>javascript everywhere</strong> - developers can use 1 programming language on both the front end and the back end of the application, using the same naming conventions, the same tools, the same best practices results in cleaner code</li> <li>[] <strong>the largest ecosystem</strong> of pre built components -</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=181s" target="_blank">[03:01</a>] Node Architecture</strong></li> <ol> <li>[] earlier, we learned that node is a javascript runtime environment</li> <li>[] there are a variety of js engines that exist, chakra in IE, spidermonkey in firefox, v8 in chrome,</li> <li>[] the different engines in each of the different browser sometimes results in javascript behaving differently in </li> <li>[] while the browser has window or document object to interact with, node does not</li> <li>[] before 2009, the only way to execute javascript code was in a web browser</li> <li>[+*] windows had/has a 'windows scripting host' which enabled the execution of js on the windows OS platform</li> <li>[+*] window 8 app development platform was based on winjs, using js + html + css to build native windows 8 apps with web technologies </li> <li>[] in 2009, ryan dahl embedded the chrome browsers js engine(v8), inside a c++ program and called it node</li> <li>[] node has no window or document object like the browser has,</li> <li>[] but node has its own set of objects that you can use to work with different things</li> <li>[] examples of node objects include things like = ] fs, ] http, ] os, ] global ] console</li> <li>[] node = v8 javascript engine + additional objects</li> <li>[]</li> <li><strong>[] node is NOT a programming language, it is NOT a framework</strong></li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=364s" target="_blank">[06:04]</a> How Node Works</strong></li> <ol> <li>[] node has a non blocking or asynchronous architecture by default</li> <li>[*] ASYNCH waiter example - ] waiter takes your order, ] goes to kitchen, ] takes next customers order, ] goes to kitchen, ] when your order is ready </li> <li>[*] SYNCH - - ] waiter takes your order, ] goes to kitchen, ] waits at kitchen until your order is ready ] returns with your order ] takes next customers order, ] ...</li> <li>[] thread allocated from server, often getting data from db takes time,</li> <li>[] if a bunch of threads are</li> <li>[] at some point, you will run out of threads and ...</li> <li>[] <strong>USE NODE for</strong> data intensive apps and realtime apps, apps with a lot of i/o,</li> <li>[] <strong>DONT USE NODE for</strong> cpu intensive apps like ] video encoding, ]</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=629s" target="_blank">[10:29</a>] Installing Node</strong></li> <ol> <li>[] use >node --version // from a command prompt, to test if you have node installed</li> <li>[] <a href="https://nodejs.org" target="_blank">nodejs.org</a> - has the latest stable version and the latest version(experimental, maybe not stable)</li> <li>[] download and install your preferred version, installer is simple and straightforward</li> </ol> <li><strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=781s">[13:01]</a> Your First Node Program</strong></li> <ol> <li>[] create first app by defining a javascript function in a file named app.js</li> <li>[] execute the program by launching node with the application file name as an arguement</li> <li>[] >node app.js</li> <li>[] demonstrates that there really is no window or document object in node</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=922s" target="_blank">[15:22]</a> Node Module System</strong></li> <ol> <li>[] what the module system is</li> <li>[] why we need them</li> <li>[] built in modules ] filesytem ] http,] events, ] os , ] global, ] ...</li> <li>[] how to create your own modules - ] myModule</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=952s" target="_blank">[15:52]</a> Global Object</strong></li> <ol> <li>[] console object is a global object, part of the global scope, it is available anywhere</li> <li>[] other global objects include methods like, ] setTimeout, ] clearTimeout ] setInterval, ] clearInterval</li> <li>[] just like the standard global js objects</li> <li>[] other node global objects</li> <li>[] remember there is no global window aka document object</li> <li>[] object called 'global' contains it global methods</li> <li>[] global.console.log -</li> <li>[] shorthand console.log -</li> <li>[] var and functions declared/defined in a module (file) are only scoped to that file, </li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=1154s" target="_blank">[19:14</a>] Modules</strong></li> <ol> <li>[] A problem with having a global scope like in browser js, is that often applications have multiple js files, any file that declares a var with the same name as declared in another file will cause problems with the application js execution</li> <li>[] create modules or functions that encapsulate specific functionality</li> <li><strong>> [] every file in a node application is considered a module</strong></li> <li>[] vars and functions defined in the file are scoped to just that file, in OOP terms they are 'private'</li> <li><strong>[] if you want to use a var or function defined in a module, outside of the file it is defined in, you need to explicitly EXPORT that var or function</strong></li> <li>[] every node application has at least 1 file, which is considered to be its 'main module'</li> <li>[] </li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=1371s" target="_blank">[22:51]</a> Creating a Module</strong></li> <ol> <li>[] using module.exports.log = log to export a function that you want to make available</li> <li>[] using module.exports.url = url to export a variable or property that you</li> <li>[] only export the required functionality, not the implementation details</li> <li>[] metaphor - dvd player - only has a few buttons on the outside to operate it, however there is lots of complexity inside the unit, what are your modules buttons</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=1655s" target="_blank">[27:35]</a> Loading a Module</strong></li> <ol> <li>[x] use require statement to load the desired module in the file that will consume it</li> <li>[x] require takes one arg, the name or path to the module file</li> <li>[*] .js extension on files is assumed by node, so it is not necessary</li> <li>[x] use ./ prefix for module files that are located in the same directory as the file that will be using the file</li> <li>[x] best practice to now use const logger = require(logger) VS using var logger = ... // prevents use from accidentally re-assiging the logger functionality</li> <li>[] using jshint -</li> <li>[x] ALSO OPTION - dont have to export as object, can export as function,</li> <li>[x] in module - module.exports = log</li> <li>[x] in consuming module - const logger = require('..'), NO need for logger.log('message'), can use by calling logger('message' )</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=1979s" target="_blank">[32:59</a>] Module Wrapper Function</strong></li> <ol> <li>[] node does not execute the code in your modules directly, it wraps your code inside of its own function wrapper</li> <li>[] wrapped inside an iife function, has args like ( exports, require, module, __filename, __dirname )</li> <li>[] console.log(__filename);</li> <li>[] node comes with a bunch of useful modules we can use</li> </ol> <li><strong>[] AD - inline</strong></li> <ol> <li>[*] inline AD for moshes <a href="https://codewithmosh.com/p/the-complete-node-js-course" target="_blank">complete course</a> , currently $29 for lifetime access , 15 hours material, restful api with node, express, mongodb, this videos content is the first segment(module) of # segments total, additional segments cover</li> <li>[] using npm,</li> <li>[] building restful apis using expressjs</li> <li>[] express advanced topics,</li> <li>[] asynch javascript,</li> <li>[] crud operations using mongoose</li> <li>[] mongo data validation</li> <li>[] mongoose modelling relationships between connected data[]</li> <li>[] authentication and authorization[73:00]</li> <li>[] handling and logging errors[63:00]</li> <li>[] unit testing [61:15]</li> <li>[] integration testing [69:00]</li> <li>[] test driven development[57:00]</li> <li>[] deployment[28:00] </li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=2393s" target="_blank">[39:53</a>] Path Module</strong></li> <ol> <li>[] a quick look at some of the common native/built in modules that come with nodejs</li> <li>[] documentation at nodejs.org -> docs -> select your version -> table of contents</li> <li>[] not everything listed here is a module,</li> <li>[] some are global objects, like the console object,</li> <li>[] common modules include [] filesystem, [] http, [] os [] path, [] process, [] querystrings, [] stream</li> <li>[] example - using the path module</li> <li>[] easier to use the path module methods then working with strings</li> <li>[1] const path = require('path');</li> <li>[*] if no path to the modulename is provided in the arg to the require methdo, node assumes that it is a built in module, if no built in module exists, node ...</li> <li>[2] let pathObject = path.parse(__filename); // filename here is the __filename of the current module</li> <li>[3] console.log(pathObject.name) // returns 'app' which is the filename without the file extension,</li> <li>[*] other 'path object' properties include root, dir, ext and base</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=2643s" target="_blank">[44:03]</a> OS Module</strong></li> <ol> <li>[x] reviewing official docs for methods available in the os module</li> <li>[x] demonstrates using os module methods to get total memory and free memory on the current system</li> <li>[x] demonstrates using template strings (vs concatenations), remember backticks and ${var}, see example below</li> <li>[<strong>x] var displayString = `the total value of the property is: {$totalValue}`</strong></li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=2902s" target="_blank">[48:22]</a> File System Module</strong></li> <ol> <li>[x] reviewing official docs for methods available in the filesystem module</li> <li>[x] almost every fs method comes in 2 forms, synch or asynch</li> <li>[*] remember the node process is a single thread, if the process is blocked waiting for something, it cant continue to serve additional requests</li> <li>[x] example - read the current directory and output a list of the files it contains</li> <li>[x] example - read the current directory and pass callback to execute, if error exists with fs call, it gets passed, can then handle error aka, else list files</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=3194s" target="_blank">[53:14</a>] Events Module</strong></li> <ol> <li>[] core concept of node, event is a signal that something has happened</li> <li>[] req</li> <li>[] docs - events module</li> <li>[] example events emitter (ee),</li> <li>[] ee is a class, note the difference in instatntiating an instance of, have to use the new operator </li> <li>[] describes object, class human, object john</li> <li><strong>[] // raise an event</strong></li> <li>[x] >emitter.emit() // making a noise, a signal, that something has happened</li> <li>[x] pass it the arguement of the name of the event,</li> <li>[x] >emitter.emit('messageLogged')</li> <li><strong>[] // NOW we need to listen for the event</strong></li> <li>[] emitter.on() || emitter.addListener()</li> <li>[] arguements for the listener include the name of the event to listen for and a callback function</li> <li><strong>[] // register a listener</strong></li> <li>[x] > emitter.on('messageLogged', function(){ console.log('message log event handler firing')})</li> <li>[!*] the order of the declaration is important</li> <li>[x] the event listener/handler must be declared before the event emitter</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=3573s" target="_blank">[59:33</a>] Event Arguments</strong></li> <ol> <li>[] you can also pass data about the event that just happenend to the event handler by using additional arguements</li> <li>[] in the event emit function, include the data about the event that you want to pass into the event handler</li> <li>[] emitter.emit('messageLogged', {id:3, name:"elm Name"});</li> <li>[] in the event handler function, accept the passed args and process them</li> <li>[] emitter.on('messageLogged', function(args){ console.log(args) }</li> <li>[] mentions using es6 arrow functions vs function keyword</li> <li>[] emitter.on('messageLogged', (args)=>{ console.log(args); }</li> <li>[] exersize = create an event for your logger</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=3763s" target="_blank">[01:02:43]</a> Extending EventEmitter</strong></li> <ol> <li>[] i put the event emitter constructs in the logging module</li> <li>[] then I raised the logging event in the logging module but the event handler was not getting called</li> <li>[] he does the same with the same result and then explains how we are working with 2 different event emitter objects, 1 in app, 1 in logging</li> <li>[] so we need to make the logger into a class</li> <li>[] make the current logger function as a method in the class</li> <li>[] and export the class (versus the function) in the module exports</li> <li>[] we then instantiate an instance of the Logger class, in the app module</li> </ol> <li><strong><a href="https://www.youtube.com/watch?v=TlB_eWDSMt4&t=4246s" target="_blank">[01:10:46]</a> HTTP Module</strong></li> <ol> <li>[x] one of the powerful building blocks of node,</li> <li>[x] can create a web server, that listens for http requests,</li> <li>[x] node docs</li> <li>[x] server is an eventEmitter, - has on method, close method and more,</li> <li>[x] inherits from .net server,</li> <li>[x] common usage, pass a callback function into create server, with request and response args,</li> <li>[x] example handling http requests in</li> <li>[x] CC my project nodeOne - app.js</li> <li>[x] wont be using this to build a server, will be using framework(expressjs) to keep things clean/modular</li> </ol></ol> <h2>[HOW-TO]</h2> <ol> <li>]</li> </ol> <h2>[REFERENCE]</h2> <ol> <li>] SRC = HN, comments</li> <li>[] book - nodejs - table of contents</li> <li>[] article - node.js - <a href="/view/article?id=4500" target="_blank">overview</a></li> </ol> <h1 style="text-align: center;"> </h1>