Created
July 1, 2020 07:11
-
-
Save cjbj/a5f6c7dd6377893cdcca1657406a4b45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const oracledb = require('oracledb'); | |
const dbConfig = require('./dbconfig.js'); | |
const httpPort = 7000; | |
async function init() { | |
try { | |
await oracledb.createPool(dbConfig); | |
// Create HTTP server and listen on port httpPort | |
const server = http.createServer(); | |
server.on('error', (err) => { | |
console.log('HTTP server problem: ' + err); | |
}); | |
server.on('request', (request, response) => { | |
handleRequest(request, response); | |
}); | |
await server.listen(httpPort); | |
console.log("Server is running at http://localhost:" + httpPort); | |
} catch (err) { | |
console.error("init() error: " + err.message); | |
} | |
} | |
async function handleRequest(request, response) { | |
response.writeHead(200, {"Content-Type": "text/xml"}); | |
if (request.url.split("/")[1] == 'favicon.ico') { // ignore requests for the icon | |
response.end(); | |
return; | |
} | |
let connection; | |
try { | |
connection = await oracledb.getConnection(); | |
const result = await connection.execute( | |
`select dbms_xmlgen.getxml(' | |
select * | |
from employees | |
order by employee_id') xml | |
from dual`, | |
[], | |
{ fetchInfo: { XML: { type: oracledb.STRING }} } | |
); | |
response.write(result.rows[0][0]); | |
} catch (err) { | |
console.error("handleRequest() error:", err); | |
} finally { | |
if (connection) { | |
try { | |
// Release the connection back to the connection pool | |
await connection.close(); | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
} | |
response.end(); | |
} | |
async function closePoolAndExit() { | |
console.log("\nTerminating"); | |
try { | |
await oracledb.getPool().close(0); | |
console.log("Pool closed"); | |
process.exit(0); | |
} catch(err) { | |
console.error(err.message); | |
process.exit(1); | |
} | |
} | |
process | |
.once('SIGTERM', closePoolAndExit) | |
.once('SIGINT', closePoolAndExit); | |
init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment