article

learn node.js in one hour

[WHAT]

  1. ] by Mosh H @ __ - 

[WHY]

[WHERE]

  1. ] WATCH THE FULL VIDEO
    1. ]

[WHEN]

  1. ] 2019-11-22

[EXAMPLE]

  1. [00:00] What is Node
    1. [] node is an open source, cross platform runtime environment for executing javascript code outside of the web browser
    2. [] node is used for rapidly prototyping and agile development
    3. [] node is used for building backend services like apis that power client applications, like web and mobile clients
    4. [] fast -
      1. [] [] node uses a non blocking
      2. [] asynchronous architecture by default
    5. [] scaleable -
      1. [] node is  built using an event driven architecture,
    6. [x] adoption - used by large companies like Paypal, walmart
    7. [] study by paypal found that company developers were able to build app twice as fast with fewer people, 33% less lines of code, 40% fewer files, 2x requests per sec and  35% faster response times
    8. [] use javascript everywhere - 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
    9. [] the largest ecosystem of pre built components -
  2. [03:01] Node Architecture
    1. [] earlier, we learned that node is a javascript runtime environment
    2. [] there are a variety of js engines that exist, chakra in IE, spidermonkey in firefox, v8 in chrome,
    3. [] the different engines in each of the different browser sometimes results in javascript behaving differently in 
    4. [] while the browser has window or document object to interact with, node does not
    5. [] before 2009, the only way to execute javascript code was in a web browser
    6. [+*] windows had/has a 'windows scripting host' which enabled the execution of js on the windows OS platform
    7. [+*] window 8 app development platform was based on winjs, using js + html + css to build native windows 8 apps with web technologies 
    8. [] in 2009, ryan dahl embedded the chrome browsers js engine(v8), inside a c++ program and called it node
    9. [] node has no window or document object like the browser has,
    10. [] but node has its own set of objects that you can use to work with different things
    11. [] examples of node objects include things like = ] fs, ] http, ] os, ] global ] console
    12. [] node = v8 javascript engine + additional objects
    13. []
    14. [] node is NOT a programming language, it is NOT a framework
  3. [06:04] How Node Works
    1. [] node has a non blocking or asynchronous architecture by default
    2. [*] ASYNCH waiter example - ] waiter takes your order, ] goes to kitchen, ] takes next customers order, ] goes to kitchen, ] when your order is ready 
    3. [*] SYNCH - - ] waiter takes your order, ] goes to kitchen, ] waits at kitchen until your order is ready ] returns with your order ] takes next customers order, ] ...
    4. [] thread allocated from server, often getting data from db takes time,
    5. [] if a bunch of threads are
    6. [] at some point, you will run out of threads and ...
    7. [] USE NODE for data intensive apps and realtime apps, apps with a lot of i/o,
    8. [] DONT USE NODE for cpu intensive apps like ] video encoding, ]
  4. [10:29] Installing Node
    1. [] use >node --version // from a command prompt, to test if you have node installed
    2. [] nodejs.org - has the latest stable version and the latest version(experimental, maybe not stable)
    3. [] download and install your preferred version, installer is simple and straightforward
  5. [13:01] Your First Node Program
    1. [] create first app by defining a javascript function in a file named app.js
    2. [] execute the program by launching node with the application file name as an arguement
    3. [] >node app.js
    4. []  demonstrates that there really is no window or document object in node
  6. [15:22] Node Module System
    1. [] what the module system is
    2. [] why we need them
    3. [] built in modules ] filesytem ] http,] events, ] os , ] global, ] ...
    4. [] how to create your own modules - ] myModule
  7. [15:52] Global Object
    1. [] console object is a global object, part of the global scope, it is available anywhere
    2. [] other global objects include methods like, ] setTimeout, ] clearTimeout ] setInterval, ] clearInterval
    3. [] just like the standard global js objects
    4. [] other node global objects
    5. [] remember there is no global window aka document object
    6. [] object called 'global' contains it global methods
    7. [] global.console.log -
    8. [] shorthand console.log -
    9. [] var and functions declared/defined in a module (file) are only scoped to that file, 
  8. [19:14] Modules
    1. [] 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
    2. [] create modules or functions that encapsulate specific functionality
    3. > [] every file in a node application is considered a module
    4. [] vars and functions defined in the file are scoped to just that file, in OOP terms they are 'private'
    5. [] 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
    6. [] every node application has at least 1 file, which is considered to be its 'main module'
    7. [] 
  9. [22:51] Creating a Module
    1. [] using module.exports.log = log to export a function that you want to make available
    2. [] using module.exports.url = url to export a variable or property that you
    3. [] only export the required functionality, not the implementation details
    4. [] 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
  10. [27:35] Loading a Module
    1. [x] use require statement to load the desired module in the file that will consume it
    2. [x] require takes one arg, the name or path to the module file
    3. [*] .js extension on files is assumed by node, so it is not necessary
    4. [x] use ./ prefix for module files that are located in the same directory as the file that will be using the file
    5. [x] best practice to now use const logger = require(logger) VS using var logger = ... // prevents use from accidentally re-assiging the logger functionality
    6. [] using jshint -
    7. [x] ALSO OPTION - dont have to export as object, can export as function,
    8. [x] in module - module.exports = log
    9. [x] in consuming module -  const logger = require('..'), NO need for logger.log('message'), can use by calling logger('message' )
  11. [32:59] Module Wrapper Function
    1. [] node does not execute the code in your modules directly, it wraps your code inside of its own function wrapper
    2. [] wrapped inside an iife function, has args like ( exports, require, module, __filename, __dirname )
    3. [] console.log(__filename);
    4. [] node comes with a bunch of useful modules we can use
  12. [] AD - inline
    1. [*] inline AD for moshes complete course , 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
    2. [] using npm,
    3. [] building restful apis using expressjs
    4. [] express advanced topics,
    5. [] asynch javascript,
    6. [] crud operations using mongoose
    7. [] mongo data validation
    8. [] mongoose modelling relationships between connected data[]
    9. [] authentication and authorization[73:00]
    10. [] handling and logging errors[63:00]
    11. [] unit testing [61:15]
    12. [] integration testing [69:00]
    13. [] test driven development[57:00]
    14. [] deployment[28:00]  
  13. [39:53] Path Module
    1. [] a quick look at some of the common native/built in modules that come with nodejs
    2. [] documentation at nodejs.org -> docs -> select your version -> table of contents
    3. [] not everything listed here is a module,
    4. [] some are global objects, like the console object,
    5. [] common modules include [] filesystem, [] http, [] os [] path, [] process, [] querystrings, [] stream
    6. [] example - using the path module
    7. [] easier to use the path module methods then working with strings
    8. [1] const path = require('path');
    9. [*] 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 ...
    10. [2] let pathObject = path.parse(__filename); // filename here is the __filename of the current module
    11. [3] console.log(pathObject.name) // returns 'app' which is the filename without the file extension,
    12. [*] other 'path object' properties include root, dir, ext and base
  14. [44:03] OS Module
    1. [x] reviewing official docs for methods available in the os module
    2. [x] demonstrates using os module methods to get total memory and free memory on the current system
    3. [x] demonstrates using template strings (vs concatenations), remember backticks and ${var}, see example below
    4. [x] var displayString = `the total value of the property is: {$totalValue}`
  15. [48:22] File System Module
    1. [x] reviewing official docs for methods available in the filesystem module
    2. [x] almost every fs method comes in 2 forms, synch or asynch
    3. [*] remember the node process is a single thread, if the process is blocked waiting for something, it cant continue to serve additional requests
    4. [x] example - read the current directory and output a list of the files it contains
    5. [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
  16. [53:14] Events Module
    1. [] core concept of node, event is a signal that something has happened
    2. [] req
    3. [] docs - events module
    4. [] example events emitter (ee),
    5. [] ee is a class, note the difference in instatntiating an instance of, have to use the new operator 
    6. [] describes object, class human, object john
    7. [] // raise an event
    8. [x] >emitter.emit() // making a noise, a signal, that something has happened
    9. [x] pass it the arguement of the name of the event,
    10. [x] >emitter.emit('messageLogged')
    11. [] // NOW we need to listen for the event
    12. [] emitter.on() || emitter.addListener()
    13. [] arguements for the listener include the name of the event to listen for and a callback function
    14. [] // register a listener
    15. [x] > emitter.on('messageLogged', function(){ console.log('message log event handler firing')})
    16. [!*] the order of the declaration is important
    17. [x] the event listener/handler must be declared before the event emitter
  17. [59:33] Event Arguments
    1. [] you can also pass data about the event that just happenend to the event handler by using additional arguements
    2. [] in the event emit function, include the data about the event that you want to pass into the event handler
    3. [] emitter.emit('messageLogged', {id:3, name:"elm Name"});
    4. [] in the event handler function, accept the passed args and process them
    5. [] emitter.on('messageLogged', function(args){ console.log(args) }
    6. [] mentions using es6 arrow functions vs function keyword
    7. [] emitter.on('messageLogged', (args)=>{ console.log(args); }
    8. [] exersize = create an event for your logger
  18. [01:02:43] Extending EventEmitter
    1. [] i put the event emitter constructs in the logging module
    2. [] then I raised the logging event in the logging module but the event handler was not getting called
    3. [] 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
    4. [] so we need to make the logger into a class
    5. [] make the current logger function as a method in the class
    6. [] and export the class (versus the function) in the module exports
    7. [] we then instantiate an instance of the Logger class, in the app module
  19. [01:10:46] HTTP Module
    1. [x] one of the powerful building blocks of node,
    2. [x] can create a web server, that listens for http requests,
    3. [x] node docs
    4. [x] server is an eventEmitter, - has on method, close method and more,
    5. [x] inherits from .net server,
    6. [x] common usage, pass a callback function into create server, with request and response args,
    7. [x] example handling http requests in
    8. [x] CC my project nodeOne - app.js
    9. [x] wont be using this to build a server, will be using framework(expressjs) to keep things clean/modular

[HOW-TO]

  1. ]

[REFERENCE]

  1. ] SRC = HN, comments
  2. [] book - nodejs - table of contents
  3. [] article - node.js - overview

 

Details Photos Edit more

Details

ID: 5762

NAME: VIDEO-TLDW-learn-node-js-in-one-hour

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,

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: -1

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and