14

Nodejs

Embed Size (px)

Citation preview

Page 1: Nodejs
Page 2: Nodejs

Node is

● Open source.● Cross-platform.● Event-driven. ● Non-blocking I/O.

Page 3: Nodejs

Built on V8

● JavaScript engine for Chrome.

● written in C++.● V8 can run standalone, or

can be embedded into any C++ application.

Page 4: Nodejs

History

● Created for Linux use in 2009.

● By Ryan Dahl ● Sponsored by Joyent.

Page 5: Nodejs

Non Blocking IO - Event Loop

Page 6: Nodejs

Hello World

setTimeout(function(){

console.log('world');

},2000);

console.log('Hello');

process.stdin.on('data', function(chunk) {

process.stdout.write('data: ' + chunk);

});

● Non-blocking IO needs different mindset and coding style.

● You are organizing your code around events.

● Nobody can tell the exact sequence of execution especially for IO events.

Page 7: Nodejs

Callback could be a hellfs.readFile(my.json', function(err, data) {

var info = JSON.parse(data);

db.findOne({id: info.id}, function(err, record) {

fs.writeFile('my.txt', record.title, function(){

console.log('Finish job');

})

})

});

Page 8: Nodejs

Http Servervar http = require('http');

http.createServer(function (request, response) {

response.writeHead(200, {'Content-Type': 'text/plain'});

response.end('Hello World\n');

}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

Import module

Use module

Page 9: Nodejs

Create Moduleexports.test = function(){

console.log("Hi I'm inside the test module");

}

var module = require('./test2.js');module.test();

● Use exports object to externalize function, object or class

● User require function to import module into your code.

Page 10: Nodejs

Package Manager - npm● NPM is the package manager for the

Node JavaScript platform● It puts modules in place so that node

can find them.● Reads all project dependencies from

package.json● Main commands are init, install, link, ls,

and search.

{ "name": "NewProject", "version": "0.0.1", "description": "showcase project", "main": "index.js", "scripts": { "test": "node index.js" }, "keywords": [ "new", "project" ], "author": "matef", "license": "BSD-2-Clause"}

Page 11: Nodejs

Frameworks - ExpressJS● Express is a minimal and flexible

Node.js web application framework.

● Express enables developer to organize artifacts and routes and views in a simple way.

● Express has a generator tool that build the typical structure for simple web project.

Page 12: Nodejs

IBM® Bluemix™

● IBM® Bluemix™ is the IBM open cloud platform.

● Bluemix™ supports Nodejs as one of its runtimes.

● Bluemix™ provides boilerplates for faster development start.

Page 13: Nodejs

Some insight into Nodejs ...

Page 14: Nodejs

It is the next big thing … catch up :)