Download presentation
Presentation is loading. Please wait.
1
Configuring Nodes Application on BlueMix
Ky Lien Solution Architect IBM Update your name and contact information Also you can personalize the title by adding their company name and logo to the slide.
2
Introduction to server-side JavaScript
Agenda Introduction to server-side JavaScript Asynchronous I/O with callback programming Express web application framework Demo local node.js application Demo node.js application on IBM BlueMix Question and Answer the fact is that by dec 2013, it crossed api shows the xponential growth of APIs, almost doubling every 5 months stores (800) ###s web sites web APIs APIs represent a new, fast-growing channel opportunity Business models are evolving 2
3
Introduction to server-side JavaScript
4
JavaScript: The language of web applications
JavaScript is the programming language for client-side web applications that run in a web browser All modern web browsers support JavaScript Developers build responsive, interactive web applications with HTML, Cascading Style Sheets (CSS), and JavaScript As an interpreted language, you do not need to compile JavaScript applications before running them You can quickly write, test, and debug JavaScript applications with a text editor and a web browser Although the language syntax resembles Java, it is not derived from the Java programming language
5
JavaScript applications in the web browser
With client-side JavaScript, developers create rich, interactive web applications that run in a web browser Web browser Application server User interface: HTML/CSS Enterprise Java components JSON/HTTP REST service Business logic: JavaScript application REST web service JavaScript applications in the web browser. With client-side JavaScript, developers create rich, interactive web applications in the web browser. In step 1, the user interface is rendered using HTML and CSS. When the user selects an option in the web page, it triggers business logic written as a JavaScript application. The JavaScript application sends a web service request using JSON over HTTP. On the server, a REST web service intercepts the call. In the last step, the application server process the web service request using a server-side application, such as Enterprise Java components.
6
JavaScript applications in the application server
With client-side JavaScript, developers create rich, interactive web applications that run in a web browser Web browser Node server User interface: HTML/CSS Node JavaScript applications JSON/HTTP REST service Business logic: JavaScript application REST web service JavaScript applications in the application server. With server-side JavaScript, Node applications process and route web service requests from the client. Compare the following diagram with the one in the previous slide. Most of the steps are identical. In step 1, the user selects an option in the user interface, written in HTML and CSS. Step 2, the option triggers a JavaScript application that implements the business logic on the client side. Step 3, the JavaScript application makes a web service call over HTTP with a data payload written in JSON. In the next step, a REST web service intercepts the HTTP request. In the final step, instead of invoking an Enterprise Java application, the Node server hosts an application written in the JavaScript language. This application runs on the server, and not in the client's web browser.
7
Node: Server-side JavaScript
Node is a server-side programming framework that uses JavaScript as its programming language Many developers are already familiar with the JavaScript language Node is built with a heavy emphasis on concurrent programming with a lightweight language Node is a single-threaded application environment that handles input/output (I/O) operations through events Instead of blocking on asynchronous I/O operations, you write callback functions to handle results when they complete Node is suited for developers that want to build scalable, concurrent server applications quickly with a minimal set of tools The IBM SDK for Node.js is an IBM package of the Node server run time The IBM SDK for Node.js is available on various hardware platforms and operating systems In addition to Microsoft Windows, Mac OS X, and Linux, IBM SDK for Node.js supports Power Systems and Linux for System z IBM SDK for Node.js Download - Node is a server-side programming framework that uses JavaScript as its programming language. Many developers are already familiar with the JavaScript language. Node is built with a heavy emphasis on concurrent programming with a lightweight language. Node is a single-threaded application environment that handles input/output (I/O) operations through events. Instead of blocking on asynchronous I/O operations, you write callback functions to handle results when they complete. on the server, and not in the client's web browser. The IBM SDK for Node.js is an IBM package of the Node server run time. The IBM SDK for Node.js is available on various hardware platforms and operating systems. In addition to Microsoft Windows, Mac OS X, and Linux, IBM SDK for Node.js supports Power Systems and Linux for System z.
8
Running node When you run the node command without any parameters, the application gives you a Read-Eval-Print-Loop (REPL) The REPL environment acts as an interactive shell for you to run JavaScript code, one line at a time Use the shell to test, inspect, and debug JavaScript applications To run a Node script, run the node command with the script name as the first parameter C:\IBM\node>node > console.log('Hello IBM Services Asset Community Call'); Hello IBM Services Asset Community Call undefined > C:\IBM\node>node helloToday.js The day of the week is Thursday. C:\IBM\node> The Node interactive shell. When you run the node command without any parameters, the application gives you a Read-Eval-Print-Loop (REPL). The REPL environment acts as an interactive shell for you to run JavaScript code, one line at a time. Use the shell to test, inspect, and debug JavaScript applications. In the example below, the Node command is executed with no additional parameters. A prompt appears. When you enter a line of JavaScript code, the Node framework interprets that line and prints the result. The last line states "undefined", meaning there is no return value from the console.log function call.
9
Packaging Node applications
A Node module is a package for a Node script file There is a one-to-one correspondence between a module and a script file To import a Node module that consists of a single script, use the require function with a relative path to the script file To import a Node module that is packaged in a subdirectory, use the require function with the name of the subdirectory Node script file Node script file require('./today') hello.js today.js Node script file Node script file require('./mod_today') hello.js /mod_today/index.js A Node module is a package for a Node script file. There is a one-to-one correspondence between a module and a script file. For example, use the require function to import a Node module var today = require('./today'); In this example, a Node script file that is named today.js in the same directory as your application. The require statement assumes that scripts have a file extension of '.js'. The require function creates an object that represents the imported Node module. When you call 'require' with the name of a subdirectory, node.js looks for a script file with the same name as the subdirectory. For example, when you call require('mod_today'), the function looks for a script file named mod_today.js. If mod_today.js does not exist, it assumes that mod_today is the name of a directory. Node.js looks for a script named index.js within the mod_today directory. [Plat_H] SPVC audio scripts: To import a Node module that consists of a single script, use the require function with a relative path to the script file. In this example, the main application is in the Node script file "hello.js". "Hello.js" makes a require function call to the today.js script file. In this example again, it is the same "hello.js" Node.js file. The Node module is saved in a directory named "mod_today". The actual script file is saved in index.js. When "hello.js: makes a call to the require function, in the "mod_today" directory, the script file checks whether there is a file named index.js. This is the default name for a script in a Node module.
10
Package.json: The module manifest
The package.json file describes details about a Node module If a module does not have a package.json file, node.js assumes that the main class is named index.js To specify a different main script for your module, specify a relative path to the Node script from the module directory The name and version fields form a unique identifier for the module For example, today-1.0.0 { "name": "mod_today", "version": "1.0.0", "main": "./lib/today", "license": "Apache-2.0" } The main field lists a path to the main node script In this example, the today.js script in the lib subdirectory Package.json defines many other fields For example, license states the module's usage rights The package.json file describes details about a Node module. If a module does not have a package.json file, node.js assumes that the main class is named index.js. To specify a different main script for your module, specify a relative path to the Node script from the module directory. This is an example of a "package.json" file. The "name" and "version" fields form a unique identifier for the module. For example, "today-1.0.0". The "main" field lists a path to the main node script. In this example, the "today.js" script in the "lib" subdirectory. Package.json defines many other fields. For example, license states the module's usage rights.
11
Exporting functions and properties from a module
Each Node module has an implicit exports object. To make a function or a value available to Node applications that import your module, add a property to exports. When you import a Node module, the require function returns a JavaScript object that represents an instance of the module To access the properties of the module, retrieve the property from the variable In the same example, today.dayOfWeek represents the current exported property from the today Node module console.log("Happy %s!", today.dayOfWeek() ); var date = new Date(); var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; exports.dayOfWeek = function () { return days[ date.getDay() - 1 ]; }; Each Node module has an implicit exports object. To make a function or a value available to Node applications that import your module, add a property to exports. In this example, the dayOfWeek property is added to the exports object. "dayOfWeek" is assigned an anonymous function that returns the day of the week. For example, if the dayOfWeek function returns 1, this value maps to "Monday". When you import a Node module, the require function returns a JavaScript object that represents an instance of the module. For example, the today variable is an instance of the today Node module. To access the properties of the module, retrieve the property from the variable. In the same example, today.dayOfWeek represents the current exported property from the today Node module.
12
Exporting functions and properties from a module
With the http Node module, you can develop an application that listens to HTTP requests and return HTTP response messages Use the http.createServer function to create an instance of a web server application Develop an anonymous function to handle the incoming request message and to send back a response message After you create an instance of a server object, set the server to listen to a specific port http.listen(8080); var server = http.createServer(function(request, response) { var body = "Hello world!"; response.writeHead( 200, { 'Content-Length': body.length, 'Content-Type': 'text/plain' }); response.end(body); server.listen(8080); With the http Node module, you can develop an application that listens to HTTP requests and return HTTP response messages. Use the http.createServer function to create an instance of a web server application. Develop an anonymous function to handle the incoming request message and to send back a response message. After you create an instance of a server object, set the server to listen to a specific port. For example, call the http.listen function on port 8080. Example: A simple HTTP server that returns a text message in an HTTP response. The http.createServer function takes 1 parameter: an anonymous function that takes in the request and response message. In the second line, the variable "body" has a value of "Hello World!". In the third line, the response.writeHead function adds the HTTP status code of 200. The "Content-Length" header field with the size of the response message body. The "Content-Type" field states a message type of "text/plain". In the next line, the response.end function closes the communications and sends the contents of the body variable as the message body. In the last line, you must call server.listen function with a port number. When this server object receives an HTTP request, it calls the anonymous function that processes the message and sends back a response HTTP mesage.
13
Asynchronous I/O with callback programming
14
Asynchronous I/O Network operations run in an asynchronous manner
For example, the response from a web service call might not return immediately When an application blocks (or waits) for a network operation to complete, that application wastes processing time on the server Node.js makes all network operations in a non-blocking manner Every network operation returns immediately To handle the result from a network call, write a callback function that Node.js calls when the network operation completes Asynchronous I/O. Network operations run in an asynchronous manner. For example, the response from a web service call might not return immediately. When an application blocks (or waits) for a network operation to complete, that application wastes processing time on the server. Node.js makes all network operations in a non-blocking manner. Every network operation returns immediately. To handle the result from a network call, write a callback function that Node.js calls when the network operation completes. and sends back a response HTTP mesage.
15
Node module calls http.request()
Application Node module http.request callback function Node.js framework Remote server Call exported function Call http.request() Send HTTP request to web service Return from exported function Return from http.request() Receive HTTP response message Process request.on('data') and request.on('end') events Print HTTP response message body to console log Node module calls http.request. In a slightly more complex scenario, your application calls some custom Node module, which in turns makes an http.request function call. The Node.js framework then calls the remote server's web service by sending an HTTP request message. In the same manner as described in the previous slide, the Node.js framework returns a value to the HTTP function call in the Node module. This response simply states that the HTTP request was successfully sent out. The Node module then returns from the exported function call. At this point, the application continues processing on to the next step, while the response message has not yet been sent. When the remote server returns an HTTP response message, the Node.js framework calls the callback function defined by the custom Node module. The purpose of the callback function is to handle two events: request.on('data') and request.on('end'). In this case, the callback function simply prints the HTTP response message body to the console log.
16
Sending an HTTP request
var options = { host: 'w1.weather.gov', path: '/xml/current_obs/KSFO.xml‘, }; http.request( options, function(response) { var buffer = ''; var result = ''; response.on('data', function(chunk) { buffer += chunk; }); response.on('end', function() { console.log(buffer); }).end(); Node.js calls this anonymous function when events occur while receiving parts of the HTTP response message Sending an HTTP request. This code example shows you how to make an HTTP request call from a function. The first parameter in the HTTP request function is an options variable. The options variable included at least two variables: the host name of the remote server, and an URL resource path that you want to act upon. In the example here, you are making a call to the U.S. National Weather Service, to retrieve the weather observation from KSFO, or San Francisco International Airport. The second parameter of the HTTP request function is a callback function. In this case, it is an anonymous function that receives one parameter: the response object. When Node.js calls this anonymous function, events occur while receiving parts of the HTTP response object. In this example, there are two specific events: a "data" event and an "end" event. For each of these two events, you define more callback functions to handle each event type. message body to the console log.
17
http.request: Options and callback parameters
The http.request function calls the callback parameter when it receives part of the HTTP response message The callback parameter is optional; you can send an HTTP request and disregard the response message http.request( options, [callback] ); When http.request calls the callback function, it passes a response object in the first parameter http.request( options, function(response) { … } ); http.request: Options and callback parameters. The http.request function calls the callback parameter when it receives part of the HTTP response message. The callback parameter is optional. You can send an HTTP request and disregard the response message. When http.request calls the callback function, it passes a response object in the first parameter of the callback function.
18
http.request: Handling response events
In Node.js, the object.on() function defines an event handler that the framework calls when an event occurs For example, the response object in the http.request callback function emits events when Node.js receives parts of the HTTP response message from the remote server response.on('data', function(chunk) { buffer += chunk; }); response.on('end', function() { console.log(buffer); Node.js emits a 'data' event when it receives a part of the HTTP response message When Node.js receives the last part of the HTTP response message, it emits an 'end' event http.request: Handling response events In Node.js, the object.on() function defines an event handler that the framework calls when an event occurs. For example, the response object in the http.request callback function emits events when Node.js receives parts of the HTTP response message from the remote server. Node.js emits a 'data' event when it receives a part of the HTTP response message. When Node.js receives the last part of the HTTP response message, it emits an 'end' event.
19
http.request: Handling error events
When you call the http.request function, it returns an http.clientRequest object With the http.clientRequest object, you can: Write data to the HTTP request message body Add headers to the HTTP request message Define an event handler for errors that Node encounters while sending the request message Call clientRequest.end() to complete sending the request var request = http.request( options, callback ); request.on('error', function(e) { resultCallback(e.message); }); request.end(); http.request: Handling error events. When you call the http.request function, it returns an http.clientRequest object. With the http.clientRequest object, you can: Write data to the HTTP request message body. Add headers to the HTTP request message. Define an event handler for errors that Node encounters while sending the request message. In this example, there is a callback handler for the error event in the request object. Call clientRequest.end() to complete sending the request.
20
Propagating errors to callback functions
As an asynchronous framework, Node.js makes extensive use of callback functions to return the result to the calling function Node.js modules in the SDK pass an error object as the first parameter in a callback function function ( error, parameter1, parameter2, … ) { … } With this convention, the callback function checks if the first parameter holds an error object If error is defined, the callback function handles the error and cleans up any open network or database connections If error is not defined, then the callback function examines the result from the call weather.current( param, function(error, temp_f) { if (error) { console.error(error); return; } console.log( "The current weather reading is %s degrees.", temp_f ); }); If the error parameter is defined, print the error Otherwise, the weather.current function call completed successfully Print the result from the function call Propagating errors to callback functions. As an asynchronous framework, Node.js makes extensive use of callback functions to return the result to the calling function. Node.js modules in the SDK pass an error object as the first parameter in a callback function. For example, the function is defined with an error as the first parameter, followed by parameter 1, parameter 2, and so on. With this convention, the callback function checks if the first parameter holds an error object. If error is defined, the callback function handles the error and cleans up any open network or database connections. If error is not defined, then the callback function examines the result from the call. Callback function with error handling In this example, the callback function takes a first parameter of error. If the error parameter is defined, print the error message. Otherwise, the weather.current function call completed successfully. Print the result from the function call.
21
http.request: Passing the HTTP response message to the calling application
In the previous example, the callback handler printed the contents of the HTTP response message body to the console One callback function for each level When one Node application calls a module in a non-blocking manner, the application must provide a callback function to process the result If the main application calls http.request(), it must provide a callback handler to process the HTTP response message If the main application calls a function that calls http.request(), there are two callback functions involved: The custom module has a callback function to handle the HTTP response message from http.request() The main application has a callback function that processes the result that is captured in the first callback function http.request: Passing the HTTP response message to the calling application In the previous example, the callback handler printed the contents of the HTTP response message body to the console. What if you wanted your callback handler to return the response message back to the original calling application? You cannot use a return function, since Node.js might call the callback function after the http.request() call completes. Rule: One callback function at each level. When one Node application calls a module in a non-blocking manner, the application must provide a callback function to process the result. If the main application calls http.request(), it must provide a callback handler to process the HTTP response message. If the main application calls a function that calls http.request(), there are two callback functions involved: The custom module has a callback function to handle the HTTP response message from http.request(). The main application has a callback function that processes the result that is captured in the first callback function. If this is confusing to you, follow this tip. Draw out a sequence diagram to map out the callback functions.
22
http.request: Create a callback function to capture the result
Application Application callback function Node module http.request callback function Node.js framework Remote server Call exported function Call http.request() Send HTTP request to web service Return from exported function Return from http.request() Receive HTTP response message Create a callback function to capture the result from the http.request function call. This sequence diagram is similar to the one that you have seen on a previous slide. The application makes a call to the Node module. In turn, the Node module makes an http.request, in order to send an HTTP request message to a remote server. Before the remote service returns an HTTP response message, the http.request function call returns control to the Node module as the request message was successfully sent. In turn, the Node module replies with a value to the main application. At this point, the remote server has not sent back an HTTP response message. At some future point, the remote server sends back an HTTP response message. The Node.js framework calls the callback function defined by the Node module. This callback function calls another callback function, one defined by the main application. Having one callback function invoke another callback function is the only way to pass a message from the Node module to the main application when it receives a response message.
23
http.request: callback function
var weather = require('./weather'); var location = 'KSFO'; weather.current( location, function( temp_f ) { console.log( temp_f ); }); When the main application calls weather.current(), it passes an anonymous callback function to process the result from the call exports.current = function ( location, resultCallback) { var options = { host: 'w1.weather.gov', path: '/xml/current_obs/' + location + '.xml‘, headers: {'User-Agent': 'weatherRequest/1.0' } }; http.request(options, function(response) { var buffer = ''; response.on('data', function(chunk) { buffer += chunk; }); response.on('end', function() { resultCallback( temp_f ); }).end(); } The resultCallback parameter stores the anonymous callback function from the main application The http.request() callback function calls resultCallback to return the result to the main application Example: Main application with callback function. When the main application calls weather.current(), it passes an anonymous callback function to process the result from the call. In this case, the anonymous function takes in one input parameter, temp_f. The purpose of this callback function is to take the weather reading in degrees Fahrenheit and print the result in the console log. Example: Node module that returns a result to the main application with a callback function. In this code example, you see a function defined for the property named "current". This property named "current" will be exported as part of the module. The anonymous function takes a parameter named "resultCallback" from the main application. This scheme is how you pass a reference to the main application's callback function to the Node module's callback function. The resultCallback parameter stores the anonymous callback function from the main application. Take a look at the bottom of the current property. There is an "response.on('end')" event handler to handle the transmission of the HTTP response message. When the remote server finishes sending back the response message, the code makes a call to resultCallback and pass it the current weather reading in degrees Fahrenheit. This is how you pass a value from one callback handler to another callback handler.
24
Express web application framework
25
Extending Node with third-party packages
The default Node framework provides a limited set of features for building web applications Developers rely on third-party packages to extend Node's features For example, Node does not provide a parsing function for XML messages In simple messages, you can parse out an XML message with JavaScript string functions You can also use an XML document object, but the object is not efficient in parsing a stream of XML data response.on('data', function(chunk) { buffer += chunk; }); response.on('end', function() { var matches = buffer.match(/\<temp_f\>.+\<\/temp_f\>/g); if ( null != matches || matches.length > 0 ) { var result = matches[0].replace(/\<temp_f\>/, "").replace(/\<\/temp_f\>/, ""); } resultCallback(null, result); Match a substring that starts and ends with the <temp_f> XML element tags Match a substring that starts and ends with the <temp_f> XML element tags The default Node framework provides a limited set of features for building web applications. Developers rely on third-party packages to extend Node's features. For example, Node does not provide a parsing function for XML messages. In simple messages, you can parse out an XML message with JavaScript string functions. You can also use an XML document object, but the object is not efficient in parsing a stream of XML data. When one Node application calls a module in a non-blocking manner, the application must provide a callback function to process the result. If the main application calls http.request(), it must provide a callback handler to process the HTTP response message. If the main application calls a function that calls http.request(), there are two callback functions involved: The custom module has a callback function to handle the HTTP response message from http.request(). The main application has a callback function that processes the result that is captured in the first callback function. If this is confusing to you, follow this tip. Draw out a sequence diagram to map out the callback functions.
26
Parse XML elements to JavaScript with xml2js
The xml2js Node package parses a string of XML elements into a JavaScript object Unlike other XML parsing Node packages, xml2js uses only in JavaScript It does not require an XML parsing library that is written in another language Third-party packages might have a different software license than the Node.js framework Confirm that the licensing terms work with your company and your application before installing the package The npm application manages Node packages in the Node framework install. C:\IBM\node>npm install xml2js node_modules\xml2js ├── └── C:\IBM\node> Install third-party packages with npm. The npm application manages Node packages in your Node framework installation. Run the npm install command to retrieve and set up a Node package and any package dependencies. In this example, you call npm install xml2js from the command line. The npm application goes out on the internet and retrieves the Node module "xml2js" as well as any dependencies that it requires. When one Node application calls a module in a non-blocking manner, the application must provide a callback function to process the result. If the main application calls http.request(), it must provide a callback handler to process the HTTP response message. If the main application calls a function that calls http.request(), there are two callback functions involved: The custom module has a callback function to handle the HTTP response message from http.request(). The main application has a callback function that processes the result that is captured in the first callback function. If this is confusing to you, follow this tip. Draw out a sequence diagram to map out the callback functions.
27
Import the package into the application
var parseString = require('xml2js').parseString; response.on('end', function() { parseString( buffer, function (error, result) { if (null == error) { console.log( result.current_observation.temp_f[0] ); } }); The parseString function runs the callback function when it finishes processing the XML tree in buffer The result JavaScript variable represents the contents of the XML fragment in buffer The property current_observation.temp_f[0] maps to the first child element in the <temp_f> XML element The <temp_f> element is a child of <current_observation> Import the package into your application. In the first line, the parseString function calls the callback function when it finishes processing the XML tree "in buffer". In the second line, the "result" JavaScript variable represents the contents of the XML fragment in "buffer". In other words, the "result" JavaScript variable represents the XML element, but in JavaScript form. The property "current_observation.temp_f[0]" maps to the first child element in the <temp_f> XML element. The <temp_f> element is a child of the <current_observation> element.
28
Express: A web application framework for Node
Express is a third-party module that provides a framework for building web applications It implements an ‘app’ class that you map to a web resource path In contrast, the standard Node.js framework treats HTTP requests at a lower, network level The http.createServer function relies on your custom callback function to parse through the web resource path The Express web application framework is one of the most popular building blocks for web applications To declare Express as a dependency, list the express module and a version number in the dependencies property "dependencies": { "express": "3.x" } { "name": "temperature", "version": "1.0.0", "description": "Retrieve current weather conditions in the United States", "main": "app.js", "dependencies": { "express": "3.x" } } Express: A web application framework for Node Express is a third-party module that provides a framework for building web applications. It implements an ‘app’ class that you map to a web resource path. In contrast, the standard Node.js framework treats HTTP requests at a lower, network level. The http.createServer function relies on your custom callback function to parse through the web resource path. The Express web application framework is one of the most popular building blocks for web applications. Declare Express as a dependency in the package manifest. The package.json file stores information about the contents of a Node module. Name: A name for the Node module. Version: A string that defines the major and minor version number of the module. Description: A sentence that describes the purpose of the module. Main: Identifies the Node script as the entry point into the module. Dependencies: Lists which Node modules the current module requires. To declare Express as a dependency, list the express module and a version number in the dependencies property.
29
Express: Run the npm command to install missing modules
When you run the npm install command inside the Node module directory, it resolves any missing dependent Node modules C:\IBM\node\temperature>npm install npm WARN package.json No repository field. npm WARN package.json No README data npm WARN package.json No license field. node_modules\express ├── ├── ├── ├── ├── ├── ├── ├── ├── ├── ├── ├── ├── ├── ├── ... Running npm install to download Express When you run the command "npm install" with no parameters, the "npm" application scans your package.json file. It checks your "node_module" directory to see if any modules are missing. In this case, the Express web application framework is not in your current installation. The "npm" application downloads the Express framework and all of its dependent modules.
30
Express: create a web application with express framework
Create an instance of the app object from the Express web application framework var current = require('../weather').current; var express = require('express'); var app = express(); app.get('/temperature/:code', function(req, res) { var param = { location: req.params.code }; current( param, function(error, temp_f) { if (error) { res.send(500, 'Error has occured: ' + error); return; } res.send("The current temperature is " + temp_f + " degrees Fahrenheit."); }); var server = app.listen(8080, function() { console.log('Listening on port %d', server.address().port); Listen to incoming HTTP GET requests to the /temperature resource path Run the weather.current function with a parameter, code, taken from the resource path app listens to incoming request on port 8080 Create a web application in your code. Once you have installed a copy of the Express web application framework, you can create an instance of the app JavaScript object from the framework. In your application, to handle web application requests, map an HTTP method and a web resource path to the JavaScript function. In this example, you are listening to incoming HTTP GET requests to the "temperature" resource path. You are also saving the value after the "temperature" resource path in a variable named "code". When you run the weather.current function, you pass the parameter "code", taken from the resource path. Create an instance of the server from the app. Call app.listen to create a web server object that listens to incoming requests on the specified port. In this example, the app listens to incoming requests on port 8080. The second parameter defines an anonymous function that the Express framework calls when it creates an instance of the server object.
31
Demo local node.js application
32
Demo node.js application on IBM BlueMix
33
Question and Answer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.