Skip to content

Instantly share code, notes, and snippets.

@petdance
Created May 17, 2019 20:56
Show Gist options
  • Save petdance/85ff68c14314b760468e3e933cab3296 to your computer and use it in GitHub Desktop.
Save petdance/85ff68c14314b760468e3e933cab3296 to your computer and use it in GitHub Desktop.
// service.js -- Reports back the ISBNs in a the given ISBN's family.
'use strict';
const oracledb = require('oracledb');
const http = require('http');
const url = require('url');
const hostname = 'hostname.com';
const port = 42000;
const lookup_sql = `-- Boring SQL`;
async function get_rows(isbn) {
var connection;
try {
connection = await oracledb.getConnection( {
user : "user",
password : 'password',
connectString : 'instance',
} );
var result = await connection.execute( lookup_sql, [isbn] );
return result.rows;
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
const server = http.createServer((req, res) => {
const isbn = url.parse(req.url,true).pathname.substr(1);
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.write('ISBN family members for ' + isbn + '\n');
const rows = get_rows( isbn );
rows.then( function(result) {
result.forEach( i => res.write( i[0] + '\n' ) );
res.end();
} );
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
$ curl http://hostname.com:42000/9780439064866
ISBN family members for 9780439064866
9780439064866
9780439064873
9780439203531
9780439420105
9780439554893
9780545582926
9780545791328
9780605754430
9780606191814
9780613287142
9780756903169
9780786222735
9781338299151
9781435238107
9781551923703
9781594130014
9781781100509
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment