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
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.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 @@
* @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;