Sample components of a gnericized version of edm00se/express-domino-nsf app on GitHub, for inclusion in my blog series (post 1/3). https://github.com/edm00se/express-domino-nsf
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
/* file: /config/index.js */ | |
var something = process.env.SOME_INFO || 'someVal'; // env var of SOME_INFO, defaulting to 'someVal' | |
module.exports = { | |
someInfo: somthing | |
}; |
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
var srvNm = process.env.DOM_SRV || ''; | |
var dbNm = process.env.DOM_DB || 'SomeApp.nsf'; | |
module.exports = { | |
db: {server:srvNm,database:dbNm} | |
}; |
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
var util = require('../util'); | |
module.exports = function (app) { | |
app.get("/beers", function(req, res, next){ | |
var resp = {}; | |
util.getView('beers',function(err,data){ | |
if(err){ | |
resp = { | |
"error": true, | |
"message": err | |
}; | |
} else { | |
resp = { | |
"error": false, | |
"data": data | |
}; | |
} | |
res.jsonp(resp); | |
}); | |
}); | |
}; |
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
var domino = require('domino-nsf'), | |
config = require('../config/db.js'), | |
db = config.db; | |
// initialize Session | |
function initSession(cb){ | |
domino.initSession(); | |
cb(); | |
}; | |
// terminate Session | |
function closeSession(){ | |
domino.termSession(); | |
}; | |
// get Document contents by UNID | |
function getDocumentByUnid(un,cb){ | |
initSession(function(){ | |
domino.getDocumentAsync(db,un,function(err,doc){ | |
if(!err){ | |
cb(null,doc); | |
}else{ | |
cb(err); | |
console.log('Error: '+err); | |
} | |
closeSession(); | |
}); | |
}); | |
}; | |
// get View contents by View name | |
function getViewContents(vw,cb){ | |
var vwOb = {view:vw,category:""}; | |
initSession(function(){ | |
domino.getViewAsync(db,vwOb,function(err,view) { | |
if(!err){ | |
cb(null,view); | |
}else{ | |
cb(err); | |
console.log('Error: '+err); | |
} | |
closeSession(); | |
}); | |
}); | |
}; | |
// exporting what we care about in a structured format | |
module.exports = { | |
getView: getViewContents, | |
getDoc: getDocumentByUnid | |
}; |
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
var path = require('path'); | |
var jarFilePath = path.join(__dirname, '../lib', 'jt400.jar'); | |
var driverNm = 'com.ibm.as400.access.AS400JDBCDriver'; | |
var addr = process.env.URL || 'as400://ibmi.fullyqualifiedname.com'; | |
var uName = process.env.ISERIES_USER_NAME || 'user'; | |
var pWord = process.env.ISERIES_PASSWORD || 'secret'; | |
// a config object w/ specifics for the connection | |
module.exports = { | |
libpath: jarFilePath, | |
drivername: driverNm, | |
url: addr, | |
user: uName, | |
password: pWord, | |
}; |
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
var util = require('../util'); | |
module.exports = function (app) { | |
app.get("/beers", function(req, res, next){ | |
var resp = {}; | |
util.query("SELECT * FROM beers", function(err, data){ | |
if(err){ | |
resp = { | |
"error": true, | |
"message": err | |
}; | |
} else { | |
resp = { | |
"error": false, | |
"data": data | |
}; | |
} | |
}); | |
res.jsonp(resp); | |
}); | |
}; |
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
var jdbc = new ( require('jdbc-pro') ); | |
var dbConf = require('../config/db.js'); | |
// handler to initialize the connection | |
function initCon(cb){ | |
jdbc.initialize(dbConf, function(err, res) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log("Connection initialized successfully!"); | |
cb(null); | |
} | |
}); | |
}; | |
// handler to close the connection | |
function closeCon(err) { | |
if(err) { | |
console.log(err); | |
} else { | |
console.log("Connection closed successfully!"); | |
} | |
}; | |
// wrapping the func, passing results or error via callback | |
function getQueryResults(sql,cb){ | |
initCon(function(err){ | |
if(err){ | |
console.log(err); | |
} else { | |
jdbc.open(function(err, conn) { | |
if (conn) { | |
// perform passed SQL statement, SELECT statement | |
jdbc.executeQuery(sql, function(err, results) { | |
if(err){ | |
console.log(err); | |
cb(err,null); | |
} else { | |
cb(null,results); | |
} | |
closeCon(); // lastly, close the connection | |
}); | |
} | |
}); | |
} | |
}); | |
}; | |
// separately wrapped func for UPDATE or INSERT | |
function getUpdateResults(sql,cb){ | |
initCon(function(err){ | |
jdbc.open(function(err, conn) { | |
if (conn) { | |
// perform passed SQL statement, INSERT or UPDATE | |
jdbc.executeUpdate(sql, function(err, results) { | |
if(err){ | |
console.log(err); | |
cb(err,null); | |
} else { | |
cb(null,results); | |
} | |
closeCon(); | |
}); | |
} | |
}); | |
}); | |
}; | |
// exporting what we care about in a structured format | |
module.exports = { | |
query: getQueryResults, | |
update: getUpdateResults | |
}; |
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
<!-- file: /public/readme.html --> | |
<!doctype html> | |
<html> | |
<head> | |
<title>Instructions</title> | |
</head> | |
<body> | |
<h3>Instructions for Use</h3> | |
<p>TODO: Describe your API endpoint's behavior and how to interact with it.</p> | |
</body> | |
</html> |
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
/* file: /routes/index.js */ | |
var path = require('path') | |
// export a function that accepts `app` as a param | |
module.exports = function (app) { | |
app.get('/', function (req, res, next) { | |
res.sendFile(path.join(__dirname, '../public', 'readme.html')); | |
}); | |
// this should be in a `beers.js` file, pulled in by require('./beers') | |
app.get('/beers', function(req, res, next){ | |
res.send('beers!'); | |
}); | |
// require('./breweries')(app); | |
// require('./vineyards')(app); | |
// add new lines for each other endpoint defined in its own file for simplicity | |
}; |
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
/* file: /server.js */ | |
var express = require('express'), | |
app = express(), | |
os = require('os'); | |
//enables and instantiates express-toobusy, which keeps it from melting under HIGH pressure | |
app.use(require('express-toobusy')()); | |
require("./routes")(app); | |
//establish simple text catch for Error 500 | |
app.use(function(err, req, res, next){ | |
console.error(err.stack); | |
res.status(500).send("<h1>Error 500</h1>\nSomething broke! The error has been logged to the server console."); | |
}); | |
//set to listen on environment port or port 3333, if no detected env var port | |
var portToListenOn = process.env.PORT || 3333; | |
app.listen(portToListenOn, function(){ | |
console.log("NodeJS serving content from "+__dirname+" on "+os.hostname()+":"+portToListenOn); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment