20
20
*
21
21
* DESCRIPTION
22
22
* 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
+ *
23
25
* Use demo.sql to create the required table or do:
24
26
* DROP TABLE mylobs;
25
27
* 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
+ *
27
31
* 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
29
33
*
30
34
*****************************************************************************/
31
35
32
- var http = require ( 'http' ) ;
33
36
var url = require ( 'url' ) ;
34
-
37
+ var http = require ( 'http' ) ;
35
38
var oracledb = require ( 'oracledb' ) ;
36
39
var dbConfig = require ( './dbconfig.js' ) ;
37
40
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 ) ;
39
64
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 ;
43
74
44
75
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
51
77
function ( err , connection )
52
78
{
53
- if ( err ) { console . error ( err . message ) ; return ; }
79
+ if ( err ) {
80
+ console . error ( err . message ) ;
81
+ return ;
82
+ }
54
83
55
84
connection . execute (
56
- "SELECT b FROM mylobs WHERE id = :id" ,
85
+ "SELECT b FROM mylobs WHERE id = :id" , // get the image
57
86
{ id : 2 } ,
58
87
function ( err , result )
59
88
{
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
+ }
62
98
63
99
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
+ }
65
104
66
105
lob . on (
67
106
'end' ,
68
107
function ( )
69
108
{
70
109
console . log ( "lob.on 'end' event" ) ;
71
- res . end ( ) ;
110
+ response . end ( ) ;
72
111
} ) ;
73
112
lob . on (
74
113
'close' ,
@@ -85,17 +124,29 @@ http.createServer(function(req, res){
85
124
{
86
125
console . log ( "lob.on 'error' event" ) ;
87
126
console . error ( err ) ;
127
+ connection . close ( function ( err ) {
128
+ if ( err ) console . error ( err ) ;
129
+ } ) ;
88
130
} ) ;
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
91
133
} ) ;
92
134
} ) ;
93
135
94
136
} 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" ) ;
97
139
}
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
+ } ) ;
99
151
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