6. 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.
7. Callback could be a hell
fs.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');
})
})
});
8. Http Server
var http = require('http');
Import module
http.createServer(function (request, response) {
Use module
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello Worldn');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
9. Create Module
exports.test = function(){
console.log("Hi I'm inside
the test module");
}
● Use exports object to
externalize function,
object or class
● User require function to
import module into your
code.
var module = require('./test2.js');
module.test();
10. 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"
}
11. 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.
12. 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.