Objectives
• After this seminar, the audiences are able to :
• Understand the basic of Node.js and common modules
• Create simple Node.js app
• Start to learn deeper
Agenda – 1 hours
• What is Node.js and how it works
• Node.js Installation
• Node.js project and module structure
• Common packages
• Host a nodejs app with PM2
What is Node.js
• Compare of JS in browser and Node.js (node apps)
Chrome Browser
V8 JS
Engine
HTML + CSS + JS
…
Node.js
Async I/O
libuv
JS
V8 JS
Engine
Event Loop
libuv
Node Bindings
(socket, http, file system,…)
Node Standard Library
chrome.exe node.exe
What is Node.js
Async I/O and Event Loop
• Node.js uses an event-driven, non-blocking I/O
model
• Aka: Callbacks
• FileSystem Api as examples:
https://nodejs.org/api/fs.html
Installation
• Just download and install (included npm tool)
• https://nodejs.org/en/download/
• Or via package-manager for linux family:
• https://nodejs.org/en/download/package-manager/
• npm: Node Package Manager
• Find, share, and reuse packages of code from hundreds of thousands of developers
• Npm install <package name> [-g ]
• -g: install to global that every node apps can use
Project Structure
• Basic structure:
• Generate package.json
npm init
• Advanced structure (use node express for web app):
• Express.js (node express): Fast, unopinionated, minimalist
web framework for Node.js
• http://expressjs.com/en/starter/generator.html
Project Structure
package.json
• Define app metadata and the starting point
• npm start in root app folder will exec node bin/www
• npm install in root app will download and install the
packages in dependences section
• The packages are stored in node_modules folder
Module
• Organize code better
• The common pattern:
• The list of module patterns:
• https://darrenderidder.github.io/talks/ModulePatterns/#/
Common packages
• Framework for creating web apps
• Express.js: it has a generator to create project, http://expressjs.com/
• Template Engines (Rendering UI for node web apps)
• EJS https://www.npmjs.com/package/ejs
• Pug (jade) https://pugjs.org/api/getting-started.html
• Mustache: https://github.com/janl/mustache.js
• Working with asynchronous JS
• Assync.js: https://github.com/caolan/async
• Promise (bluebird), http://bluebirdjs.com/docs/getting-started.html
• Working with database
• Mongoose: http://mongoosejs.com/,
• mysql: https://github.com/mysqljs/mysql
• SQL Server: https://www.npmjs.com/package/mssql
Host a node app with PM2
• PM2 - production process manager for Node.js applications
• with a built-in load balancer
• keep applications alive forever
• reload them without downtime
• and to facilitate common system admin tasks
• Install PM2
• pm2 start <startpoint.js>
• Ref: https://github.com/Unitech/pm2
Others
• IDE: any text editor, Visual Studio Code, notepad++, net beans…
• ES6: ECMAScript 6 (aka ECMAScript 2015)
• New features of JavaScript language (and including previous version - ES5)
• Tutorial: http://ccoenraets.github.io/es6-tutorial/ (new syntax, let, lamda, class, module,
promise...)
• Unit Test:
• get start: https://www.codementor.io/nodejs/tutorial/unit-testing-nodejs-tdd-mocha-
sinon
• CI: ???
• Flow control:
• http://blog.vullum.io/javascript-flow-callback-hell-vs-async-vs-highland/