+ All Categories
Home > Documents > Introduction to NodeJS

Introduction to NodeJS

Date post: 08-Feb-2016
Category:
Upload: xaria
View: 73 times
Download: 1 times
Share this document with a friend
Description:
Introduction to NodeJS. What is the fuzz all about?. Learning & Development. http://academy.telerik.com. Telerik School Academy. Table of Contents. Overview of NodeJS Building and installing NodeJS Developing IDEs What is the Event Loop? Writing code with callbacks Modules - PowerPoint PPT Presentation
Popular Tags:
25
Introduction to NodeJS What is the fuzz all about? Learning & Development http://academy.telerik.com Telerik School Academy
Transcript
Page 1: Introduction to  NodeJS

Introduction to NodeJS

What is the fuzz all about?

Learning & Developmenthttp://academy.telerik.com

Telerik School Academy

Page 2: Introduction to  NodeJS

Table of Contents1. Overview of NodeJS

1.Building and installing NodeJS2.Developing IDEs3.What is the Event Loop?4.Writing code with callbacks

2. Modules1.Using modules2.Installing modules

2

Page 3: Introduction to  NodeJS

Overview of NodeJS

Page 4: Introduction to  NodeJS

Overview of NodeJS Background

Page 5: Introduction to  NodeJS

Why NodeJS Node is written in JavaScript

One language on the server and the client

Full control of the server Asynchronous and fast (callback oriented)

Page 6: Introduction to  NodeJS

Building Blocks & Installation

NodeJS libuv – high-performance event I/O

library V8 – Google Chrome's JavaScript

engine JavaScript -> C++

Installation http://nodejs.org/ Run Command Prompt (cmd) Type "node" and run it

Page 8: Introduction to  NodeJS

The Event Loop

Page 9: Introduction to  NodeJS

The Event Loop

Page 10: Introduction to  NodeJS

Asynchronous Code Standard way

Callback approach

var conn = getDbConnection(connectionString);var stmt = conn.createStatement();var results = stmt.executeQuery(sqlQuery);for (var i=0; i<results.length; i++) { // print results[i];}

getDbConnection(connectionString, function(err, conn) { conn.createStatement(function(err, stmt) { var results = stmt.executeQuery(sqlQuery); results.on(‘row’, function(result) { // print result }); });});

Page 11: Introduction to  NodeJS

Asynchronous Code Standard way

Callback approach

var conn = getDbConnection(connectionString);var stmt = conn.createStatement();var results = stmt.executeQuery(sqlQuery);for (var i=0; i<results.length; i++) { // print results[i];}

getDbConnection(connectionString, function(err, conn) { conn.createStatement(function(err, stmt) { var results = stmt.executeQuery(sqlQuery); results.on(‘row’, function(result) { // print result }); });});

Page 12: Introduction to  NodeJS

Asynchronous Code Convention

Callback is last parameter in the async call

Error is first parameter in the callback

var handleResults = function(error, results) { // if error is undefined… // do something with the results}

getStuff(inputParam, handleResults);

Page 13: Introduction to  NodeJS

Asynchronous Code For simple uses – anonymous function

Closures are your friend

Do not overuse!

getStuff(inputParam, function(error, results) { // if error is undefined… // do something with the results});

someOtherFunction(function(err, stuffToGet) { var foo = 23; getStuff(stuffToGet, function(error, results) { // if error is undefined… // do something with the results (and foo) });});

Page 14: Introduction to  NodeJS

Asynchronous CodeLive Demo

Page 15: Introduction to  NodeJS

Using Modules

Page 16: Introduction to  NodeJS

Using Modules

Modules are used with "require"var first = require('first');var Second = require('second');var justPart = require('largeModule').justPart;

var propertyResult = 2 + first.property; // export variablevar functionResult = first.function() * 3; // export function

var second = new Second(); // export object

console.log(justPart()); // export part of object

Page 17: Introduction to  NodeJS

Built-in Modules Built-in modules

Come with Node Are "require"-ed with string

identifier

Commonly used modules fs, http, crypto, os More at http://nodejs.org/api/

var fs = require('fs');

Page 18: Introduction to  NodeJS

Built-in ModulesLive Demo

Page 19: Introduction to  NodeJS

Your Modules

Each .js file is a different module Are "require"-ed with file system

semantics ".js" is not needed in the stringvar data = require('./data'); // in same directoryvar a = require('./other/a'); // in child directoryvar b = require('../lib/b'); // in parent directory's childvar justPart = require(‘./data’).part; // just part of module

Page 20: Introduction to  NodeJS

Your Modules Variable are exported with module.exports// first.js

var count = 2; var doIt = function(i, callback) { … } module.exports.doIt = doIt; module.exports.someVar = 'result';

// second.js

var one = require('./first');one.doIt(23, function (err, result) { console.log(result);});console.log(one.someVar);console.log(one.count); // invalid

Page 21: Introduction to  NodeJS

Your ModulesLive Demo

Page 22: Introduction to  NodeJS

Third-Party Modules Third-Party Modules

Installed from Node Package Manager (NPM)

Command: "npm install mdl_name" Are "require"-ed with string

identifier

Some modules have command line tools

Command: "npm install –g mdl_name" Example: Express, Mocha

var request = require('request');

Page 23: Introduction to  NodeJS

Third-Party ModulesLive Demo

Page 24: Introduction to  NodeJS

Resources

http://nodejs.org/ - NodeJS official web site

http://nodejs.org/api/ - API documentation

http://blog.nodejitsu.com/npm-cheatsheet - NPM documentation

https://npmjs.org/ - NPM official web site

https://github.com/felixge/node-style-guide - NodeJS style guide

Page 25: Introduction to  NodeJS

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезанияASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET

курсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране

Николай Костов - блог за програмиранеC# курс, програмиране, безплатно

?? ? ?

??? ?

?

? ?

??

?

?

? ?

Questions?

?

Introduction to NodeJS

http://academy.telerik.com


Recommended