Intro to Node.js
Albert@Dec 2017
Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
Node.js uses an event-driven, non-blocking I/O model that makes it
lightweight and efficient.
Node.js' package ecosystem, npm, is the largest ecosystem of open source
libraries in the world.
Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
Node.js uses an event-driven, non-blocking I/O model that makes it
lightweight and efficient.
Node.js' package ecosystem, npm, is the largest ecosystem of open source
libraries in the world.
JavaScript Runtime
Browser JavaScript: Core(ECMAScript), DOM, BOM
Node.js: Core(ECMAScript), modules
JavaScript Runtime
Node.js Core:
•Google V8 JS engine
•Libuv library (support event loop in windows & *nix )
•protocol bindings (sockets & http)
JavaScript Runtime built on V8
V8 Engine comprises into 3 major component:
•Compiler : dissects the JS code
•Optimizer : create AST which further converts to SSA
•Garbage Collector
•old spaces new spaces in heap memory
•keeps track of JS objects
•-- max-old-space-size & -- max-new-space-size
JavaScript Runtime built on V8
Garbage Collector :
Event-driven, non-blocking I/O model
Event-driven, non-blocking I/O model
Event-driven, non-blocking I/O model
•timers : exe callbacks scheduled by timers
•I/O callbacks : exe callbacks except timers、close、
setImmediate;
•idle, prepare : used internally
•poll : retrieve new I/O events
•check : exe setImmediate callbacks
•close callbacks : e.g. socket.on('close', ...)
Event-driven, non-blocking I/O model
Poll phase without timers :
•poll quenue is not empty ---- exe callbacks
•poll quenue is empty
•If setimmediate ---- check phase
•If no setimmediate ---- wait for added callback
Poll phase with timers :
•Exe timers when poll quenue is empty
Modules
CommonJS
Modules
CommonJS
•Cache
•Core modules (fs http path)
•Path modules
•Custom modules(node_modules)
Modules
CommonJS
Start server
Start server with framework
More framework
•Express
•Meteor
•Koa
•ThinkJS
•…
More Stack
•Framework: express@4.X Koa@2.0
•Database: mongoose node-postgres
•Asynchronous control: bluebird(Promise/A+) generator async
•View engine: ejs jade
•Test: mocha ava
•Debug: node-inpector VSCode
More Package
•NPM
More Version
Node.js 8 Moves into Long-Term Support and Node.js 9 Becomes the New Current
Release Line
Version Control & ES feature check
•NVM
•Node green
Events.emit(‘Thanks’)
&&
console.log(‘End’);