Download - Nodejs getting started
![Page 1: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/1.jpg)
Welcome!Node.js Getting Started
<@Triet Ho/>
![Page 2: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/2.jpg)
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
![Page 3: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/3.jpg)
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
![Page 4: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/4.jpg)
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/Olibuv
JS
V8 JS Engine
Event Looplibuv
Node Bindings(socket, http, file system,…)
Node Standard Library
chrome.exe node.exe
![Page 5: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/5.jpg)
What is Node.jsHello World!• File: demo.js
• Run demo.js
![Page 6: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/6.jpg)
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
![Page 7: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/7.jpg)
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
![Page 8: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/8.jpg)
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
![Page 9: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/9.jpg)
Project Structurepackage.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
![Page 10: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/10.jpg)
Module• Organize code better
• The common pattern:
• The list of module patterns:
• https://darrenderidder.github.io/talks/ModulePatterns/#/
![Page 11: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/11.jpg)
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
![Page 12: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/12.jpg)
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
![Page 13: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/13.jpg)
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/
![Page 14: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/14.jpg)
Questions?
![Page 15: Nodejs getting started](https://reader034.vdocuments.net/reader034/viewer/2022051122/58f092321a28ab40028b4639/html5/thumbnails/15.jpg)
Cafe!