© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using...

19
© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using node.js on Bluemix IBM Ecosystem Development Instructors : Krishnaprasad Subbarao, Abhay Ratnaparkhi

Transcript of © 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using...

© 2014 IBM Corporation

Empowering the IBM ecosystem

Introduction to Developing applications using node.js on Bluemix

IBM Ecosystem DevelopmentInstructors :

– Krishnaprasad Subbarao,Abhay Ratnaparkhi

© 2014 IBM Corporation

Empowering the IBM ecosystem

• Introduction to node.js

– What is node.js

– node.js first app

– Components of node.js

– node.js api

– node.js modules

– Express

– npm

• Developing a node.js application on Bluemix

– IBM DevOps Services for Bluemix

• Developing IOT application on Bluemix

Agenda

© 2014 IBM Corporation

Empowering the IBM ecosystem

• open source, cross-platform runtime environment for server-side and networking applications

• Server side Java script system

• applications are written in JavaScript, and can be run within the Node.js runtime.

• contains a built-in library to allow applications to act as a Web server

• Powered by Google 8 runtime

• provides an event-driven architecture and a non-blocking I/O API

• commonly used for real-time web applications.

What is node.js

© 2014 IBM Corporation

Empowering the IBM ecosystem

• Download link : http://nodejs.org/download/

• First program HelloWord.js

var http = require('http');

http.createServer(function (req, res) {

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

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

}).listen(1337, "127.0.0.1");

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

• Cmd> node HelloWorld.js

Server running at http://127.0.0.1:1337/

• http://localhost:1337/

node.js first app

© 2014 IBM Corporation

Empowering the IBM ecosystem

node.js : Components

© 2014 IBM Corporation

Empowering the IBM ecosystem

• Application : the user developed application

• Java script : prototype based scripting language

• V8 Javascript Engine :

– Open source javascript engine developed by Google

– Used by Chrome, Node.js

• Bindings : programs that make V8 and the library able to talk to each other.

– e.g. binding for database libraries

– https://pravinchavan.wordpress.com/2013/11/08/c-binding-with-node-js/

node.js Components continued..

© 2014 IBM Corporation

Empowering the IBM ecosystem

• LIBUV :

– a multi-platform support library

– focuss on asynchronous I/O

– primarily developed for use by Node.js

– also used by Luvit, Julia, pyuv and few others

– Event loop

• Non thread safe

• Handles – long lived objects e.g. a TCP server handle

– Call backs are called when an event occurs

• Requests

– Short lived operations run directly on the loop

node.js Components continued..

© 2014 IBM Corporation

Empowering the IBM ecosystem

• Provides large number of built in functions, modules, add ons

• http://nodejs.org/api/index.html

• HTTP

– provides a way to develop an http server which can process http requests

• Globals

– require : A reference to a function used to require modules.

– module : A reference to the current module

– exports :A reference to the module.exports variable

– console : For printing to stdout and stderr.

node.js api

© 2014 IBM Corporation

Empowering the IBM ecosystem

• A Node/npm module is just an simple JavaScript file

• follows the CommonJS module spec.

• encapsulates related code into a single unit of code

• Allows building re-usable code

• Samples modules : http, express

node.js modules

© 2014 IBM Corporation

Empowering the IBM ecosystem

• // greetings.js

sayHelloInEnglish = function() {

return "Hello";

};

sayHelloInSpanish = function() {

return "Hola";

};

node.js modules continued..

© 2014 IBM Corporation

Empowering the IBM ecosystem

• // greetings.js

// var exports = module.exports = {};

exports.sayHelloInEnglish = function() {

return "HELLO";

};

exports.sayHelloInSpanish = function() {

return "Hola";

};

node.js modules continued..

© 2014 IBM Corporation

Empowering the IBM ecosystem

• // main.js

var greetings = require("./greetings.js");

• Equivalent to

var greetings = {

sayHelloInEnglish: function() {

return "HELLO";

},

sayHelloInSpanish: function() {

return "Hola";

}

};

node.js modules continued..

© 2014 IBM Corporation

Empowering the IBM ecosystem

• Express :

– minimal and flexible Node.js web application framework

– provides a robust set of features for web and mobile applications.

– http://expressjs.com/

– Provides 4 objects

• Application - app

– holds the settings of the express application

• HTTP request – req

– provides a way to read all the request parameters, query string, request body for POST requests

Express

© 2014 IBM Corporation

Empowering the IBM ecosystem

• Express :

– Objects

• HTTP response - res

– provides ways to return the response back to the client.

• Router – Router

– accepts a set of functions attached to a path

– executed when a request for the attached path or any inner path is received

Express continued..

© 2014 IBM Corporation

Empowering the IBM ecosystem

• HelloWorldExpress.js

var express = require('express')

var app = express()

app.get('/', function (req, res) {

res.send('Hello World!')

})

var server = app.listen(3000, function () {

var host = server.address().address

var port = server.address().port

console.log('Example app listening at http://%s:%s', host, port)

})

Express continued..

© 2014 IBM Corporation

Empowering the IBM ecosystem

• pre-installed package manager for the Node.js server platform.

• It is used to install Node.js programs from the npm registry

• npm init - creates a new package

• npm install

• e.g. npm install express –save

– Install express and adds to package.json

npm

© 2014 IBM Corporation

Empowering the IBM ecosystem

• Contains complete structure of the application.

– all packaging, dependency and version related information.

• important contents

– main : module ID that is the primary entry point to your program

– License : license information of the module.

– man : to specify the help contents about this package

– config : to set configuration parameters used in package scripts

– Dependencies : list modules on which this module is dependent

Package.json

© 2014 IBM Corporation

Empowering the IBM ecosystem

{

"name": "samples",

"version": "1.0.0",

"description": "Node.js introduction samples",

"main": "HelloWorld-express.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"license": "ibm",

"dependencies": {

"express": "^4.11.0"

}

}

Package.json

© 2014 IBM Corporation

Empowering the IBM ecosystem

Developing a node.js application on Bluemix