/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * * You may not use the identified files except in compliance with the Apache * License, Version 2.0 (the "License.") * * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0. * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * limitations under the License. * * NAME * webapp.js * * DESCRIPTION * Shows a web based query using connections from connection pool. * * This displays a table of employees in the specified department. * * The script creates an HTTP server listening on port 7000 and * accepts a URL parameter for the department ID, for example: * http://localhost:7000/90 * * Uses Oracle's sample HR schema. Scripts to create the HR schema * can be found at: https://github.com/oracle/db-sample-schemas * *****************************************************************************/ var http = require('http'); var url = require('url'); var oracledb = require('oracledb'); var dbConfig = require('./dbconfig.js'); var portid = 7000; // HTTP listening port number // Main entry point. Creates a connection pool, on callback creates an // HTTP server and executes a query based on the URL parameter given // The pool values are arbitrary for the sake of showing how to set the properties. oracledb.createPool ( { user : dbConfig.user, password : dbConfig.password, connectString : dbConfig.connectString, poolMax : 44, poolMin : 2, poolIncrement : 5, poolTimeout : 4 }, function(err, pool) { if (err) { console.error("createPool() callback: " + err.message); return; } // Create HTTP server and listen on port - portid hs = http.createServer ( function(request, response) // Callback gets HTTP request & response object { var urlparts = request.url.split("/"); var deptid = urlparts[1]; htmlHeader(response, "Oracle Database Driver for Node.js" , "Example using node-oracledb driver"); if (deptid != parseInt(deptid)) { handleError(response, 'URL path "' + deptid + '" is not an integer. Try http://localhost:' + portid + '/30', null); return; } // Checkout a connection from the pool pool.getConnection ( function(err, connection) { if (err) { handleError(response, "getConnection() failed ", err); return; } // console.log("Connections open: " + pool.connectionsOpen); // console.log("Connections in use: " + pool.connectionsInUse); connection.execute( "SELECT employee_id, first_name, last_name " + "FROM employees " + "WHERE department_id = :id", [deptid], // bind variable value function(err, result) { if (err) { connection.release( function(err) { if (err) { handleError(response, "execute() error release() callback", err); return; } }); handleError(response, "execute() callback", err); return; } displayResults(response, result, deptid); /* Release the connection back to the connection pool */ connection.release( function(err) { if (err) { handleError(response, "normal release() callback", err); return; } }); htmlFooter(response); }); }); }); hs.listen(portid, "localhost"); console.log("Server running at http://localhost:" + portid); }); // Report an error function handleError(response, text, err) { if (err) { text += err.message } console.error(text); response.write("

Error: " + text + "

"); htmlFooter(response); } // Display query results function displayResults(response, result, deptid) { response.write("

" + "Employees in Department " + deptid + "

"); response.write(""); // Column Title response.write(""); for (col = 0; col < result.metaData.length; col++) { response.write(""); } response.write(""); // Rows for (row = 0; row < result.rows.length; row++) { response.write(""); for (col = 0; col < result.rows[row].length; col++) { response.write(""); } response.write(""); } response.write("
" + result.metaData[col].name + "
" + result.rows[row][col] + "
"); } // Prepare HTML header function htmlHeader(response, title, caption) { response.writeHead (200, {"Content-Type" : "text/html" }); response.write (""); response.write (""); response.write (""); response.write ("\n"); response.write ("" + caption + ""); response.write (""); response.write (""); response.write ("

" + title + "

"); } // Prepare HTML footer function htmlFooter(response) { response.write("\n"); response.end(); }