2222 * Listens for an HTTP request and returns an image queried from a BLOB column 
2323 * Also shows the connection pool's caching using a 'default' pool. 
2424 * 
25-  * Use demo.sql to create the required table or do: 
26-  * DROP TABLE mylobs; 
27-  * CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB); 
28-  * 
29-  * Run lobinsert1.js to load an image before running this example. 
30-  * 
3125 * Start the listener with 'node blobhttp.js' and then use a browser 
3226 * to load http://localhost:7000/getimage 
3327 * 
3428 * This example uses Node 8's async/await syntax. 
3529 * 
3630 *****************************************************************************/ 
3731
38- var  url  =  require ( 'url' ) ; 
39- var  http  =  require ( 'http' ) ; 
40- var  oracledb  =  require ( 'oracledb' ) ; 
41- var  dbConfig  =  require ( './dbconfig.js' ) ; 
32+ const  url  =  require ( 'url' ) ; 
33+ const  http  =  require ( 'http' ) ; 
34+ const  oracledb  =  require ( 'oracledb' ) ; 
35+ const  dbConfig  =  require ( './dbconfig.js' ) ; 
36+ const  demoSetup  =  require ( './demosetup.js' ) ; 
4237
43- var  httpPort  =  7000 ; 
38+ const  httpPort  =  7000 ; 
4439
4540// Main entry point. Creates a connection pool which becomes the 
4641// 'default' pool, and then creates an HTTP server. 
4742async  function  init ( )  { 
4843 try  { 
49-  await  oracledb . createPool ( 
50-  { 
51-  user : dbConfig . user , 
52-  password : dbConfig . password , 
53-  connectString : dbConfig . connectString 
54-  } ) ; 
44+  await  oracledb . createPool ( dbConfig ) ; 
5545 console . log ( 'Connection pool started' ) ; 
5646
47+  // create the demo table 
48+  const  connection  =  await  oracledb . getConnection ( ) ; 
49+  await  demoSetup . setupLobs ( connection ,  true ) ; 
50+  await  connection . close ( ) ; 
51+ 
5752 // Create HTTP server and listen on port httpPort 
5853 const  server  =  http . createServer ( ) ; 
5954 server . on ( 'error' ,  ( err )  =>  { 
@@ -63,7 +58,7 @@ async function init() {
6358 handleRequest ( request ,  response ) ; 
6459 } ) ; 
6560 await  server . listen ( httpPort ) ; 
66-  console . log ( "Server running. Try requesting:  http://localhost:"  +  httpPort  +  "/getimage" ) ; 
61+  console . log ( "Server is  running. Try loading  http://localhost:"  +  httpPort  +  "/getimage" ) ; 
6762
6863 }  catch  ( err )  { 
6964 console . error ( 'init() error: '  +  err . message ) ; 
@@ -84,14 +79,14 @@ async function handleRequest(request, response) {
8479 connection  =  await  oracledb . getConnection ( ) ;  // gets a connection from the 'default' connection pool 
8580
8681 const  result  =  await  connection . execute ( 
87-  "SELECT b FROM mylobs  WHERE id = :id" ,  // get the image 
82+  "SELECT b FROM no_lobs  WHERE id = :id" ,  // get the image 
8883 {  id : 2  } 
8984 ) ; 
9085 if  ( result . rows . length  ===  0 )  { 
91-  throw  new  Error ( "No results. Did you run lobinsert1.js? " ) ; 
86+  throw  new  Error ( "No data selected from table. " ) ; 
9287 } 
9388
94-  var  lob  =  result . rows [ 0 ] [ 0 ] ; 
89+  const  lob  =  result . rows [ 0 ] [ 0 ] ; 
9590 if  ( lob  ===  null )  { 
9691 throw  new  Error ( "BLOB was NULL" ) ; 
9792 } 
@@ -117,6 +112,7 @@ async function handleRequest(request, response) {
117112
118113 }  catch  ( err )  { 
119114 console . error ( err ) ; 
115+  await  closePoolAndExit ( ) ; 
120116 }  finally  { 
121117 if  ( connection )  { 
122118 try  { 
@@ -137,9 +133,9 @@ async function closePoolAndExit() {
137133 console . log ( '\nTerminating' ) ; 
138134 try  { 
139135 // Get the pool from the pool cache and close it when no 
140-  // connections are in use, or force it closed after 10  seconds 
136+  // connections are in use, or force it closed after 2  seconds 
141137 // If this hangs, you may need DISABLE_OOB=ON in a sqlnet.ora file 
142-  await  oracledb . getPool ( ) . close ( 10 ) ; 
138+  await  oracledb . getPool ( ) . close ( 2 ) ; 
143139 console . log ( 'Pool closed' ) ; 
144140 process . exit ( 0 ) ; 
145141 }  catch ( err )  { 
0 commit comments