Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions generators/app/templates/app/config/route-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ function registerRoutes(application) {
var route = getRoute(routeItem);
var method = getMethod(routeItem);

registerRoute(application, controller, route, method);
if (method instanceof Array) {
method.forEach(function (entry) {
registerRoute(application, controller, route, entry);
})
} else {
registerRoute(application, controller, route, method);
}
}

createConfigRoute(application);
Expand Down Expand Up @@ -66,20 +72,32 @@ function getMethod(routeItem) {
throw 'Undefined or empty "method" property in "lib/config/route.config.json"';
}

var method = routeItem.method.toLowerCase();

switch(method) {
case 'get':
case 'put':
case 'post':
case 'delete':
return method;
break;
default:
throw 'Invalid REST "method" property in "lib/config/route.config.json": ' + method;
var method;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be cleaner, there is code duplication here that could easily be refactored to move the code in the switch statement into another function.


if (routeItem.method instanceof Array) {
var method = [];
routeItem.method.forEach(function (entry) {
method.push(validateMethod(entry.toLowerCase()));
});
return method;
} else {
return validateMethod(routeItem.method.toLowerCase());
}
}

function validateMethod(method) {
switch (method.toLowerCase()) {
case 'get':
case 'put':
case 'post':
case 'delete':
return method.toLowerCase();
break;
default:
throw 'Invalid REST "method" property in "lib/config/route.config.json": ' + method;
}
}

function registerRoute(application, controller, route, method) {
application.route(route)[method](function(req, res, next) {
controller[method](req, res, next);
Expand Down