From 95be1b15b5f77407ecc8eafe56068a2a845aff44 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Tue, 5 Feb 2019 12:10:28 -0500 Subject: [PATCH 1/2] Add support for PATCH, PUT, and DELETE methods Signed-off-by: Kevin Turcios --- template/node10-express-armhf/index.js | 3 +++ template/node10-express/index.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/template/node10-express-armhf/index.js b/template/node10-express-armhf/index.js index 499846c..8c85f48 100644 --- a/template/node10-express-armhf/index.js +++ b/template/node10-express-armhf/index.js @@ -82,6 +82,9 @@ var middleware = (req, res) => { app.post('/*', middleware); app.get('/*', middleware); +app.patch('/*', middleware); +app.put('/*', middleware); +app.delete('/*', middleware); const port = process.env.http_port || 3000; diff --git a/template/node10-express/index.js b/template/node10-express/index.js index 499846c..8c85f48 100644 --- a/template/node10-express/index.js +++ b/template/node10-express/index.js @@ -82,6 +82,9 @@ var middleware = (req, res) => { app.post('/*', middleware); app.get('/*', middleware); +app.patch('/*', middleware); +app.put('/*', middleware); +app.delete('/*', middleware); const port = process.env.http_port || 3000; From 1c1a9d7f5191ed84580fa0fc9512fed3f055294d Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Sun, 3 Mar 2019 19:13:20 -0500 Subject: [PATCH 2/2] Fix issue for fail method not returning custom status codes Signed-off-by: Kevin Turcios --- template/node10-express-armhf/index.js | 35 ++++++++++++-------------- template/node10-express/index.js | 35 ++++++++++++-------------- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/template/node10-express-armhf/index.js b/template/node10-express-armhf/index.js index 8c85f48..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)) { @@ -82,9 +80,6 @@ var middleware = (req, res) => { app.post('/*', middleware); app.get('/*', middleware); -app.patch('/*', middleware); -app.put('/*', middleware); -app.delete('/*', middleware); const port = process.env.http_port || 3000; @@ -99,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 8c85f48..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)) { @@ -82,9 +80,6 @@ var middleware = (req, res) => { app.post('/*', middleware); app.get('/*', middleware); -app.patch('/*', middleware); -app.put('/*', middleware); -app.delete('/*', middleware); const port = process.env.http_port || 3000; @@ -99,3 +94,5 @@ let isArray = (a) => { let isObject = (a) => { return (!!a) && (a.constructor === Object); }; + +module.exports = app; \ No newline at end of file