2020 *
2121 * DESCRIPTION
2222 * Listens for an HTTP request and returns an image queried from a BLOB column
23+ * Also shows the connection pool's caching using a 'default' pool.
24+ *
2325 * Use demo.sql to create the required table or do:
2426 * DROP TABLE mylobs;
2527 * CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
26- * Run blobinsert1.js to load an image before running this example.
28+ *
29+ * Run lobinsert1.js to load an image before running this example.
30+ *
2731 * Start the listener with 'node blobhttp.js' and then use a browser
28- * to load http://127.0.0.1 :7000/getimage
32+ * to load http://localhost :7000/getimage
2933 *
3034 *****************************************************************************/
3135
32- var http = require ( 'http' ) ;
3336var url = require ( 'url' ) ;
34-
37+ var http = require ( 'http' ) ;
3538var oracledb = require ( 'oracledb' ) ;
3639var dbConfig = require ( './dbconfig.js' ) ;
3740
38- var portid = 7000 ;
41+ var httpPort = 7000 ;
42+
43+ // Main entry point. Creates a connection pool which becomes the
44+ // 'default' pool. The callback creates an HTTP server.
45+ function init ( ) {
46+ oracledb . createPool (
47+ {
48+ user : dbConfig . user ,
49+ password : dbConfig . password ,
50+ connectString : dbConfig . connectString
51+ } ,
52+ function ( err ) {
53+ if ( err ) {
54+ console . error ( "createPool() error: " + err . message ) ;
55+ return ;
56+ }
57+
58+ // Create HTTP server and listen on port 'httpPort'
59+ http
60+ . createServer ( function ( request , response ) {
61+ handleRequest ( request , response ) ;
62+ } )
63+ . listen ( httpPort ) ;
3964
40- http . createServer ( function ( req , res ) {
41- var request = url . parse ( req . url , true ) ;
42- var action = request . pathname ;
65+ console . log ( "Server running. Try requesting: http://localhost:" + httpPort + "/getimage" ) ;
66+ } ) ;
67+ }
68+
69+ // Handles each web request
70+ function handleRequest ( request , response ) {
71+
72+ var requrl = url . parse ( request . url , true ) ;
73+ var action = requrl . pathname ;
4374
4475 if ( action == '/getimage' ) {
45- oracledb . getConnection (
46- {
47- user : dbConfig . user ,
48- password : dbConfig . password ,
49- connectString : dbConfig . connectString
50- } ,
76+ oracledb . getConnection ( // gets a connection from the 'default' connection pool
5177 function ( err , connection )
5278 {
53- if ( err ) { console . error ( err . message ) ; return ; }
79+ if ( err ) {
80+ console . error ( err . message ) ;
81+ return ;
82+ }
5483
5584 connection . execute (
56- "SELECT b FROM mylobs WHERE id = :id" ,
85+ "SELECT b FROM mylobs WHERE id = :id" , // get the image
5786 { id : 2 } ,
5887 function ( err , result )
5988 {
60- if ( err ) { console . error ( err . message ) ; return ; }
61- if ( result . rows . length === 0 ) { console . log ( "No results" ) ; return ; }
89+ if ( err ) {
90+ console . error ( err . message ) ;
91+ return ;
92+ }
93+
94+ if ( result . rows . length === 0 ) {
95+ console . error ( "No results. Did you run lobinsert1.js?" ) ;
96+ return ;
97+ }
6298
6399 var lob = result . rows [ 0 ] [ 0 ] ;
64- if ( lob === null ) { console . log ( "BLOB was NULL" ) ; return ; }
100+ if ( lob === null ) {
101+ console . log ( "BLOB was NULL" ) ;
102+ return ;
103+ }
65104
66105 lob . on (
67106 'end' ,
68107 function ( )
69108 {
70109 console . log ( "lob.on 'end' event" ) ;
71- res . end ( ) ;
110+ response . end ( ) ;
72111 } ) ;
73112 lob . on (
74113 'close' ,
@@ -85,17 +124,29 @@ http.createServer(function(req, res){
85124 {
86125 console . log ( "lob.on 'error' event" ) ;
87126 console . error ( err ) ;
127+ connection . close ( function ( err ) {
128+ if ( err ) console . error ( err ) ;
129+ } ) ;
88130 } ) ;
89- res . writeHead ( 200 , { 'Content-Type' : 'image/jpeg' } ) ;
90- lob . pipe ( res ) ;
131+ response . writeHead ( 200 , { 'Content-Type' : 'image/jpeg' } ) ;
132+ lob . pipe ( response ) ; // write the image out
91133 } ) ;
92134 } ) ;
93135
94136 } else {
95- res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
96- res . end ( ' Try requesting: http://127.0.0.1:7000 /getimage\n' ) ;
137+ response . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
138+ response . end ( " Try requesting: http://localhost:" + httpPort + " /getimage\n" ) ;
97139 }
98- } ) . listen ( portid , '127.0.0.1' ) ;
140+ }
141+
142+ process
143+ . on ( 'SIGTERM' , function ( ) {
144+ console . log ( "\nTerminating" ) ;
145+ process . exit ( 0 ) ;
146+ } )
147+ . on ( 'SIGINT' , function ( ) {
148+ console . log ( "\nTerminating" ) ;
149+ process . exit ( 0 ) ;
150+ } ) ;
99151
100- console . log ( "Server running at http://127.0.0.1:" + portid ) ;
101- console . log ( "Try requesting: http://127.0.0.1:7000/getimage" ) ;
152+ init ( ) ;
0 commit comments