[] node is an open source, cross platform runtime environment for executing javascript code outside of the web browser
[] node is used for rapidly prototyping and agile development
[] node is used for building backend services like apis that power client applications, like web and mobile clients
[] fast -
[] scaleable -
[x] used by large companies like Paypal, walmart
[] 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
[] 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
[] the largest ecosystem of pre built components -
[] node has a non blocking or asynchronous architecture by default
[*] ASYNCH waiter example - ] waiter takes your order, ] goes to kitchen, ] takes next customers order, ] goes to kitchen, ] when your order is ready
[*] SYNCH - - ] waiter takes your order, ] goes to kitchen, ] waits at kitchen until your order is ready ] returns with your order ] takes next customers order, ] ...
[] thread allocated from server, often getting data from db takes time,
[] if a bunch of threads are
[] at some point, you will run out of threads and ...
[] USE NODE for data intensive apps and realtime apps, apps with a lot of i/o,
[] DONT USE NODE for cpu intensive apps like ] video encoding, ]
[] 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
[] create modules or functions that encapsulate specific functionality
> [] every file in a node application is considered a module
[] vars and functions defined in the file are scoped to just that file, in OOP terms they are 'private'
[] 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
[] every node application has at least 1 file, which is considered to be its 'main module'
[] using module.exports.log = log to export a function that you want to make available
[] using module.exports.url = url to export a variable or property that you
[] only export the required functionality, not the implementation details
[] 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
[x] use require statement to load the desired module in the file that will consume it
[x] require takes one arg, the name or path to the module file
[*] .js extension on files is assumed by node, so it is not necessary
[x] use ./ prefix for module files that are located in the same directory as the file that will be using the file
[x] best practice to now use const logger = require(logger) VS using var logger = ... // prevents use from accidentally re-assiging the logger functionality
[] using jshint -
[x] ALSO OPTION - dont have to export as object, can export as function,
[x] in module - module.exports = log
[x] in consuming module - const logger = require('..'), NO need for logger.log('message'), can use by calling logger('message' )
[] node does not execute the code in your modules directly, it wraps your code inside of its own function wrapper
[] wrapped inside an iife function, has args like ( exports, require, module, __filename, __dirname )
[] console.log(__filename);
[] node comes with a bunch of useful modules we can use
[] AD - inline
[*] 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
[] using npm,
[] building restful apis using expressjs
[] express advanced topics,
[] asynch javascript,
[] crud operations using mongoose
[] mongo data validation
[] mongoose modelling relationships between connected data[]
[] a quick look at some of the common native/built in modules that come with nodejs
[] documentation at nodejs.org -> docs -> select your version -> table of contents
[] not everything listed here is a module,
[] some are global objects, like the console object,
[] common modules include [] filesystem, [] http, [] os [] path, [] process, [] querystrings, [] stream
[] example - using the path module
[] easier to use the path module methods then working with strings
[1] const path = require('path');
[*] 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 ...
[2] let pathObject = path.parse(__filename); // filename here is the __filename of the current module
[3] console.log(pathObject.name) // returns 'app' which is the filename without the file extension,
[*] other 'path object' properties include root, dir, ext and base
[x] reviewing official docs for methods available in the filesystem module
[x] almost every fs method comes in 2 forms, synch or asynch
[*] remember the node process is a single thread, if the process is blocked waiting for something, it cant continue to serve additional requests
[x] example - read the current directory and output a list of the files it contains
[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