
Node.js - the core Mark Volkmann [email protected] Object Computing, Inc. April 21, 2012 Overview ... “Node's goal is to provide an easy way to build scalable network programs.” http://nodejs.org/#about A full programming environment, not just for building “servers” “The official name of Node is "Node". The unofficial name is "Node.js" to disambiguate it from other nodes.” https://github.com/joyent/node/wiki/FAQ Runs on top of Chrome V8 (see next slide) Implemented in C++ and JavaScript Supported on Linux, Mac OS X and Windows passed control of the project Created by Ryan Dahl at Joyent to Isaac Schlueter on 1/30/12 a cartoon from substack Node.js 2 ... Overview Event-based rather than thread-based; can use multiple processes Assumes most time consuming operations involve I/O invoked asynchronously; non-blocking a callback function is invoked when they complete Node.js 3 Should You Use It? Reasons To Use application can benefit from asynchronous, non-blocking I/O application is not compute-intensive V8 engine is fast enough prefer callback or actor models of concurrency over thread-based approach with synchronized access to mutable state same language on client and server like dynamically typed languages large number of JavaScript developers Some issues being addressed finding packages - there are a large number of them and finding the best ones isn’t easy enough debugging - stack traces from asynchronously executed code are incomplete event loop - sometimes difficult to determine why a program isn’t exiting typically due to open connections Overview 4 Multiple Threads & Processes Node uses multiple threads internally to simulate non-blocking file I/O You can’t create new threads unless you use “Threads A GoGo” https://github.com/xk/node-threads-a-gogo “provides an asynchronous, evented and/or continuation passing style API for moving blocking/longish CPU-bound tasks out of Node's event loop to JavaScript threads that run in parallel in the background and that use all the available CPU cores automatically; all from within a single Node process” Can use multiple, cooperating processes see “Child Processes” core module processes created with fork function can emit and listen for messages see “Clusters” core module “easily create a network of processes that all share server ports” Node.js 5 Chrome V8 From Google Used by Chrome browser and Node.js Implemented in C++ Currently supports ECMAScript 5 Node adopts the JavaScript syntax supported by V8 so will support ES6 when V8 supports it Node.js 6 Where To Look For Functionality 1. JavaScript core classes: Arguments, Array, Boolean, Date, Error, Function, Global, JSON, Math, Number, Object, RegExp, String 2. Core Modules included with Node http://nodejs.org/docs/latest/api/ view source at https://github.com/joyent/node JavaScript is in lib directory C++ code is in src directory 3. Userland Modules (third party) typically installed using NPM tool http://search.npmjs.org/ 8802 NPM packages on 4/12/12 4. Write yourself Packages have JavaScript APIs, but can be partially implemented in C++. Node.js 7 Event Loop When a Node program starts, it automatically starts an event loop node name.js The currently running function, or the main script, can add function calls to the event queue one way is by passing a function to process.nextTick When the currently running function completes next function in event queue is removed from queue and run U.K. roller coaster Most asynchronous functions, such as those that perform I/O (1930 to 2007) take a callback function as an argument add a call to that function to the event queue when their work completes Program ends when event queue is empty and there are no open network connections Node.js 8 Synchronous vs. Asynchronous Asynchronous functions preferred over synchronous in most cases, especially when time to complete is long or unpredictable take a callback function invoke it, passing an error description and possibly additional arguments Synchronous functions can make application unresponsive if long running do not take a callback function if an error occurs, throw an error description either a string or an Error object throwing an Error is preferred because when strings are thrown, no stacktrace is available Node.js 9 Callbacks Functions passed to asynchronous functions often anonymous Invoked any number of times typically just once when operation completes Parameter that accepts callback by convention, last parameter by convention, named cb or callback Callback parameters typically an object describing an error, if any, and a result by convention, error is first argument and is named err Some libraries require following these conventions ex. Async.js See example on next slide Node.js 10 Callback Example var fs = require('fs'); JavaScript { demo.json "name": "Mark Volkmann", function readObject(filePath, cb) { "address": { fs.readFile(filePath, function (err, buf) { "street": "644 Glen Summit", var obj = null; "city": "St. Charles", if (!err) { "state": "Missouri", try { "zip": 63304 obj = JSON.parse(buf); // can throw }, } catch (e) { "hobby": "running" err = e; } } } fs = require 'fs' CoffeeScript cb(err, obj); }); readObject = (filePath, cb) -> } fs.readFile filePath, (err, buf) -> if !err readObject('demo.json', function (err, obj) { try if (err) { obj = JSON.parse(buf) # can throw console.error(err); catch e } else { err = e console.log(obj); cb err, obj } }); readObject 'demo.json', (err, obj) -> if err console.error err else console.log obj Node.js 11 Async Userland Module Provides many functions that simplify writing asynchronous code Arguably the most popular Node flow control library Functions three categories collections foreach[Series|Limit], map[Series], filter[Series], reject[Series], reduce[Right], detect[Series], sortBy, some, every, concat[Series] control flow series, parallel, whilst, until, waterfall, queue, auto, iterator, apply, nextTick utilities memoize, unmemoize, log, dir, noConflict Written by Caolan McMahon https://github.com/caolan/async Node.js 12 Async Example var async = require('async'); var fs = require('fs'); var rimraf = require('rimraf'); // Unix "rm -rf" for Node var dirPath = 'foo'; var fileName = 'bar.txt'; var filePath = dirPath + '/' + fileName; var content = 'some content'; async.waterfall( [ rimraf.bind(null, dirPath), fs.mkdir.bind(null, dirPath), fs.writeFile.bind(null, filePath, content), fs.stat.bind(null, filePath) ], function (err, stats) { if (err) { throw err; } console.log('size is ' + stats.size); }); Node.js 13 Node Globals (other than standard JavaScript globals) Variables defined outside functions are global in browsers are local to current module in Node Node global variables console - used to write to stdout and stderr global - object that holds most global properties and functions can use to share properties across modules; values can be functions process - has methods that get info about and interact with current process; extends EventEmitter more on this later require - has property cache (see next slide) Buffer - constructor function for creating objects that read and write data, especially binary data Node global functions require, setTimeout, clearTimeout, setInterval, clearInterval require is a function that has properties Node.js 14 Node Local Variables Node variables that are local to current module __dirname full path to directory that contains the module source file __filename full path to source file name that defines the module module object that holds information about the current module shared by all instances of current module main property of interest is exports exports object used to export properties from a module; values can be functions same as module.exports require.cache a property on the require function The require function has other properties, but they are rarely used directly. holds required modules so each is only loaded once They include: extensions, main, delete a property to allow a module to be reloaded registerExtension and resolve. by a subsequent call to require property is full path to module, ex. delete require.cache[__dirname + '/mymodule.js']; Node.js 15 Modules Defined by a single JavaScript file may “require” others that are their own modules Top-level variables and functions defined in them are local to the module not global in the entire runtime like in a browser environment not visible to other modules unless exported Each module has it’s own local variable named “module” that refers to an object with these properties exports - initially set to {}; see next slide parent - module object of module that required this one filename - full path to file that defines this module loaded - false until first require of the module completes; defaults to false paths - array of filepaths that would be searched to find this module exited - no longer used children - no longer used Node.js 16 Defining Modules A module can expose functions to other modules by exporting them not visible outside module if not exported can also export non-function values, including objects and arrays, To export many functions but that isn’t as common exports.name = some-function; repeat to export additional things To export a single function can be a constructor function; module.exports = some-function; can have properties whose values are other functions replaces the default exports object exports only one thing from the module not used in conjunction with previous kind of exports A Node package is a collection of one or more JavaScript modules, Should also create package.json and README.md optional C++ source files, optional shell scripts and used by npm used by GitHub a package.json file that describes the contents of the package and identifies the main module (or uses index.js by default). Node.js 17 Using Modules var name = require('module-name'); 1. searches core modules searches for specified name, 2. searches directories listed in NODE_PATH environment variable then tries these file extensions: .js, .json, .node • delimited with : in Linux and Mac or ; in Windows 3.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages124 Page
-
File Size-