diff --git a/template/node10-express-armhf/index.js b/template/node10-express-armhf/index.js index 499846c..fe65713 100644 --- a/template/node10-express-armhf/index.js +++ b/template/node10-express-armhf/index.js @@ -26,17 +26,17 @@ class FunctionEvent { class FunctionContext { constructor(cb) { - this.value = 200; this.cb = cb; + this.statusCode; this.headerValues = {}; } - status(value) { - if(!value) { - return this.value; + status(statusCode) { + if(!statusCode) { + return this.statusCode; } - this.value = value; + this.statusCode = statusCode; return this; } @@ -46,25 +46,23 @@ class FunctionContext { } this.headerValues = value; - return this; + return this; } - succeed(value) { - let err; - this.cb(err, value); + succeed(message) { + this.cb(200, message); } - fail(value) { - let message; - this.cb(value, message); + fail(message) { + this.cb(500, message); } } var middleware = (req, res) => { - let cb = (err, functionResult) => { - if (err) { - console.error(err); - return res.status(500).send(err); + let cb = (statusCode, functionResult) => { + // If fnContext.status() has not been called, use the default status code of 200 (succeed), or 500 (fail) + if (!fnContext.status()) { + fnContext.status(statusCode); } if(isArray(functionResult) || isObject(functionResult)) { @@ -96,3 +94,5 @@ let isArray = (a) => { let isObject = (a) => { return (!!a) && (a.constructor === Object); }; + +module.exports = app; \ No newline at end of file diff --git a/template/node10-express/index.js b/template/node10-express/index.js index 499846c..fe65713 100644 --- a/template/node10-express/index.js +++ b/template/node10-express/index.js @@ -26,17 +26,17 @@ class FunctionEvent { class FunctionContext { constructor(cb) { - this.value = 200; this.cb = cb; + this.statusCode; this.headerValues = {}; } - status(value) { - if(!value) { - return this.value; + status(statusCode) { + if(!statusCode) { + return this.statusCode; } - this.value = value; + this.statusCode = statusCode; return this; } @@ -46,25 +46,23 @@ class FunctionContext { } this.headerValues = value; - return this; + return this; } - succeed(value) { - let err; - this.cb(err, value); + succeed(message) { + this.cb(200, message); } - fail(value) { - let message; - this.cb(value, message); + fail(message) { + this.cb(500, message); } } var middleware = (req, res) => { - let cb = (err, functionResult) => { - if (err) { - console.error(err); - return res.status(500).send(err); + let cb = (statusCode, functionResult) => { + // If fnContext.status() has not been called, use the default status code of 200 (succeed), or 500 (fail) + if (!fnContext.status()) { + fnContext.status(statusCode); } if(isArray(functionResult) || isObject(functionResult)) { @@ -96,3 +94,5 @@ let isArray = (a) => { let isObject = (a) => { return (!!a) && (a.constructor === Object); }; + +module.exports = app; \ No newline at end of file