Upload
mahmoud-atef
View
93
Download
0
Embed Size (px)
Citation preview
Node is
● Open source.● Cross-platform.● Event-driven. ● Non-blocking I/O.
Built on V8
● JavaScript engine for Chrome.
● written in C++.● V8 can run standalone, or
can be embedded into any C++ application.
History
● Created for Linux use in 2009.
● By Ryan Dahl ● Sponsored by Joyent.
Non Blocking IO - Event Loop
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.
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');
})
})
});
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
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.
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"}
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.
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.
Some insight into Nodejs ...
It is the next big thing … catch up :)