This is an automated email from the ASF dual-hosted git repository.

mrutkowski pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/incubator-openwhisk-runtime-nodejs.git


The following commit(s) were added to refs/heads/master by this push:
     new 61ca90c  Add use() method to short-circuit requests with no route 
handlers (#113)
61ca90c is described below

commit 61ca90c1614a9d6920f7f3a91cea82a870613a07
Author: Matt Rutkowski <[email protected]>
AuthorDate: Thu Mar 7 13:51:06 2019 -0600

    Add use() method to short-circuit requests with no route handlers (#113)
    
    * Fix Node Express 'use()' method by removing invalid 'err' parameter
    
    * Add use() method to short-circuit requests with no handlers
    
    * Add use() method to short-circuit requests with no handlers
---
 core/nodejsActionBase/app.js         | 17 ++++++++++++-----
 core/nodejsActionBase/src/service.js |  1 -
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/core/nodejsActionBase/app.js b/core/nodejsActionBase/app.js
index 7dbc433..9e28fa6 100644
--- a/core/nodejsActionBase/app.js
+++ b/core/nodejsActionBase/app.js
@@ -38,16 +38,23 @@ app.use(bodyParser.json({ limit: "48mb" }));
 app.post('/init', wrapEndpoint(service.initCode));
 app.post('/run',  wrapEndpoint(service.runCode));
 
-app.use(function(err, req, res, next) {
-    console.error(err.stack);
-    res.status(500).json({ error: "Bad request." });
-  });
+// short-circuit any requests to invalid routes (endpoints) that we have no 
handlers for.
+app.use(function (req, res, next) {
+    res.status(500).json({error: "Bad request."});
+});
+
+// register a default error handler. This effectively only gets called when 
invalid JSON is received (JSON Parser)
+// and we do not wish the default handler to error with a 400 and send back 
HTML in the body of the response.
+app.use(function (err, req, res, next) {
+    console.log(err.stackTrace);
+    res.status(500).json({error: "Bad request."});
+});
 
 service.start(app);
 
 /**
  * Wraps an endpoint written to return a Promise into an express endpoint,
- * producing the appropriate HTTP response and closing it for all controlable
+ * producing the appropriate HTTP response and closing it for all controllable
  * failure modes.
  *
  * The expected signature for the promise value (both completed and failed)
diff --git a/core/nodejsActionBase/src/service.js 
b/core/nodejsActionBase/src/service.js
index 26dca1b..d124ae4 100644
--- a/core/nodejsActionBase/src/service.js
+++ b/core/nodejsActionBase/src/service.js
@@ -58,7 +58,6 @@ function NodeActionService(config) {
      * @param app express app
      */
     this.start = function start(app) {
-        var self = this;
         server = app.listen(app.get('port'), function() {
             var host = server.address().address;
             var port = server.address().port;

Reply via email to