change default of ignoreTrailingSlash to true
[squeep-api-dingus] / lib / dingus.js
index 0057685815472ace39dd086957da6a4b6251bbd3..0f2af748eca503201a9c2e08be97e1bae31462ba 100644 (file)
@@ -13,7 +13,7 @@ const querystring = require('querystring');
 const common = require('./common');
 const ContentNegotiation = require('./content-negotiation');
 const Enum = require('./enum');
-const { DingusError, ResponseError } = require('./errors');
+const { DingusError, ResponseError, RouterNoMethodError, RouterNoPathError } = require('./errors');
 const { extensionToMime } = require('./mime-helper');
 const Router = require('./router');
 const Template = require('./template');
@@ -22,7 +22,7 @@ const Template = require('./template');
 const _fileScope = common.fileScope(__filename);
 
 const defaultOptions = {
-  ignoreTrailingSlash: false,
+  ignoreTrailingSlash: true,
   proxyPrefix: '',
   strictAccept: true,
   selfBaseUrl: '',
@@ -36,7 +36,7 @@ class Dingus {
   /**
    * @param {Object} logger object which implements logging methods
    * @param {Object} options
-   * @param {Boolean} options.ignoreTrailingSlash 
+   * @param {Boolean} options.ignoreTrailingSlash requests for '/foo/' will match a '/foo' route
    * @param {string} options.proxyPrefix leading part of url path to strip
    * @param {Boolean} options.strictAccept whether to error on unsupported Accept type
    * @param {string} options.selfBaseUrl for constructing links
@@ -268,18 +268,13 @@ class Dingus {
     try {
       ({ handler, handlerArgs } = this.router.lookup(req.method, pathPart, ctx));
     } catch (e) {
-      if (e instanceof DingusError) {
-        switch (e.message) {
-          case 'NoPath':
-            handler = this.handlerNotFound.bind(this);
-            break;
-          case 'NoMethod':
-            handler = this.handlerMethodNotAllowed.bind(this);
-            break;
-          default:
-            this.logger.error(_scope, 'unknown dingus error', { error: e });
-            handler = this.handlerInternalServerError.bind(this);
-        }
+      if (e instanceof RouterNoPathError) {
+        handler = this.handlerNotFound.bind(this);
+      } else if (e instanceof RouterNoMethodError) {
+        handler = this.handlerMethodNotAllowed.bind(this);
+      } else if (e instanceof DingusError) {
+        this.logger.error(_scope, 'unknown dingus error', { error: e });
+        handler = this.handlerInternalServerError.bind(this);
       } else if (e instanceof URIError) {
         handler = this.handlerBadRequest.bind(this);
       } else {
@@ -313,16 +308,11 @@ class Dingus {
    * Parse rawBody as contentType into ctx.parsedBody.
    * @param {string} contentType
    * @param {object} ctx
-   * @param {string|buffer}
+   * @param {string|buffer} rawBody
   */
   parseBody(contentType, ctx, rawBody) {
     const _scope = _fileScope('parseBody');
 
-    if (!rawBody) {
-      // 1.2.4 and earlier expected rawBody on context
-      rawBody = ctx.rawBody;
-    }
-
     switch (contentType) {
       case Enum.ContentType.ApplicationForm:
         ctx.parsedBody = this.querystring.parse(rawBody);