add missing 'lib/common.js'

This commit is contained in:
AJ ONeal 2017-06-08 22:16:28 +00:00
parent 7b0e2ac85b
commit 360a53b84a
1 changed files with 38 additions and 0 deletions

38
lib/common.js Normal file
View File

@ -0,0 +1,38 @@
'use strict';
module.exports.rejectableRequest = function rejectableRequest(req, res, promise, msg) {
return promise.error(function (err) {
res.error(err);
}).catch(function (err) {
console.error('[ERROR] \'' + msg + '\'');
console.error(err.message);
console.error(err.stack);
res.error(err);
});
};
module.exports.promisableRequest =
module.exports.promiseRequest = function promiseRequest(req, res, promise, msg) {
return promise.then(function (result) {
if (result._cache) {
res.setHeader('Cache-Control', 'public, max-age=' + (result._cache / 1000));
res.setHeader('Expires', new Date(Date.now() + result._cache).toUTCString());
}
if (result._mime) {
res.setHeader('Content-Type', result._mime);
}
if (result._value) {
result = result._value;
}
res.send(result);
}).error(function (err) {
res.error(err);
}).catch(function (err) {
console.error('[ERROR] \'' + msg + '\'');
console.error(err.message);
console.error(err.stack);
res.error(err);
});
};