Skip to content

Commit f610e1b

Browse files
committed
Rework web app example to return HTTP 500 code on error
1 parent eaa084c commit f610e1b

File tree

1 file changed

+60
-43
lines changed

1 file changed

+60
-43
lines changed

examples/webapp.js

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@
3030
*
3131
* In production applications, set poolMin=poolMax (and poolIncrement=0)
3232
*
33-
* This example requires node-oracledb 3 or later.
33+
* This example requires node-oracledb 5 or later.
3434
*
3535
* This example uses Node 8's async/await syntax.
3636
*
3737
*****************************************************************************/
3838

39+
// On Windows and macOS, you can specify the directory containing the Oracle
40+
// Client Libraries at runtime, or before Node.js starts. On other platforms
41+
// the system library search path must always be set before Node.js is started.
42+
// See the node-oracledb installation documentation.
43+
// If the search path is not correct, you will get a DPI-1047 error.
44+
//
45+
// oracledb.initOracleClient({ libDir: 'C:\\instantclient_19_8' }); // Windows
46+
// oracledb.initOracleClient({ libDir: '/Users/your_username/Downloads/instantclient_19_8' }); // macOS
47+
3948
// If you increase poolMax, you must increase UV_THREADPOOL_SIZE before Node.js
4049
// starts its thread pool. If you set UV_THREADPOOL_SIZE too late, the value is
4150
// ignored and the default size of 4 is used.
@@ -50,6 +59,9 @@ const demoSetup = require('./demosetup.js');
5059

5160
const httpPort = 7000;
5261

62+
// If additionally using Database Resident Connection Pooling (DRCP), then set a connection class:
63+
// oracledb.connectionClass = 'MYAPPNAME';
64+
5365
// Main entry point. Creates a connection pool and an HTTP server
5466
// that executes a query based on the URL parameter given.
5567
// The pool values shown are the default values.
@@ -71,9 +83,9 @@ async function init() {
7183
// poolTimeout: 60, // terminate connections that are idle in the pool for 60 seconds
7284
// queueMax: 500, // don't allow more than 500 unsatisfied getConnection() calls in the pool queue
7385
// queueTimeout: 60000, // terminate getConnection() calls queued for longer than 60000 milliseconds
74-
// sessionCallback: myFunction, // function invoked for brand new connections or by a connection tag mismatch
86+
// sessionCallback: initSession, // function invoked for brand new connections or by a connection tag mismatch
7587
// stmtCacheSize: 30, // number of statements that are cached in the statement cache of each connection
76-
// _enableStats: false // record pool usage statistics that can be output with pool._logStats()
88+
// _enableStats: false // record pool usage statistics that can be output with oracledb.getPool()._logStats()
7789
});
7890

7991
// create the demo table
@@ -97,18 +109,20 @@ async function init() {
97109
}
98110
}
99111

112+
// initSession() is configured by the pool sessionCallback property.
113+
// It will be invoked internally when each brand new pooled connection
114+
// is first used. See the sessionfixup.js and sessiontaggingX.js examples.
115+
/*
116+
function initSession(connection, requestedTag, cb) {
117+
connection.execute(`ALTER SESSION SET ...'`, cb);
118+
}
119+
*/
120+
100121
async function handleRequest(request, response) {
101122
const urlparts = request.url.split("/");
102123
const id = urlparts[1];
103124

104-
htmlHeader(
105-
response,
106-
"Banana Farmer Demonstration",
107-
"Example using node-oracledb driver"
108-
);
109-
110125
if (id == 'favicon.ico') { // ignore requests for the icon
111-
htmlFooter(response);
112126
return;
113127
}
114128

@@ -134,7 +148,12 @@ async function handleRequest(request, response) {
134148
[id] // bind variable value
135149
);
136150

137-
displayResults(response, result, id);
151+
displayResults(
152+
response,
153+
"Banana Farmer Demonstration",
154+
"Example using node-oracledb driver",
155+
result,
156+
id);
138157

139158
} catch (err) {
140159
handleError(response, "handleRequest() error", err);
@@ -148,24 +167,31 @@ async function handleRequest(request, response) {
148167
}
149168
}
150169
}
151-
htmlFooter(response);
152-
}
153-
154-
// Report an error
155-
function handleError(response, text, err) {
156-
if (err) {
157-
text += ": " + err.message;
158-
}
159-
console.error(text);
160-
response.write("<p>Error: " + text + "</p>");
161170
}
162171

163172
// Display query results
164-
function displayResults(response, result, id) {
173+
function displayResults(response, title, caption, result, id) {
174+
175+
response.writeHead(200, {"Content-Type": "text/html"});
176+
response.write("<!DOCTYPE html>");
177+
response.write("<html>");
178+
response.write("<head>");
179+
response.write("<style>" +
180+
"body {background:#FFFFFF;color:#000000;font-family:Arial,sans-serif;margin:40px;padding:10px;font-size:12px;text-align:center;}" +
181+
"h1 {margin:0px;margin-bottom:12px;background:#FF0000;text-align:center;color:#FFFFFF;font-size:28px;}" +
182+
"table {border-collapse: collapse; margin-left:auto; margin-right:auto;}" +
183+
"td, th {padding:8px;border-style:solid}" +
184+
"</style>\n");
185+
response.write("<title>" + caption + "</title>");
186+
response.write("</head>");
187+
response.write("<body>");
188+
response.write("<h1>" + title + "</h1>");
189+
165190
response.write("<h2>" + "Harvest details for farmer " + id + "</h2>");
191+
166192
response.write("<table>");
167193

168-
// Column Title
194+
// Column Titles
169195
response.write("<tr>");
170196
for (let col = 0; col < result.metaData.length; col++) {
171197
response.write("<th>" + result.metaData[col].name + "</th>");
@@ -181,29 +207,20 @@ function displayResults(response, result, id) {
181207
response.write("</tr>");
182208
}
183209
response.write("</table>");
184-
}
185210

186-
// Prepare HTML header
187-
function htmlHeader(response, title, caption) {
188-
response.writeHead(200, {"Content-Type": "text/html"});
189-
response.write("<!DOCTYPE html>");
190-
response.write("<html>");
191-
response.write("<head>");
192-
response.write("<style>" +
193-
"body {background:#FFFFFF;color:#000000;font-family:Arial,sans-serif;margin:40px;padding:10px;font-size:12px;text-align:center;}" +
194-
"h1 {margin:0px;margin-bottom:12px;background:#FF0000;text-align:center;color:#FFFFFF;font-size:28px;}" +
195-
"table {border-collapse: collapse; margin-left:auto; margin-right:auto;}" +
196-
"td, th {padding:8px;border-style:solid}" +
197-
"</style>\n");
198-
response.write("<title>" + caption + "</title>");
199-
response.write("</head>");
200-
response.write("<body>");
201-
response.write("<h1>" + title + "</h1>");
211+
response.write("</body>\n</html>");
212+
response.end();
213+
202214
}
203215

204-
// Prepare HTML footer
205-
function htmlFooter(response) {
206-
response.write("</body>\n</html>");
216+
// Report an error
217+
function handleError(response, text, err) {
218+
if (err) {
219+
text += ": " + err.message;
220+
}
221+
console.error(text);
222+
response.writeHead(500, {"Content-Type": "text/html"});
223+
response.write(text);
207224
response.end();
208225
}
209226

0 commit comments

Comments
 (0)