Skip to content

Instantly share code, notes, and snippets.

@cmstead
Created April 11, 2016 04:26
Show Gist options
  • Select an option

  • Save cmstead/9353f8cd5f2de44f10f6b05a446efdd0 to your computer and use it in GitHub Desktop.

Select an option

Save cmstead/9353f8cd5f2de44f10f6b05a446efdd0 to your computer and use it in GitHub Desktop.
'use strict';
var routeFunctions = {
delete: {},
get: {},
post: {},
put: {}
};
function routeStub(method, path, action) {
routeFunctions[method][path] = action;
}
function throwOnUndefined(value, message) {
if(typeof value === 'undefined') {
throw new Error(message);
}
}
function throwOnBadKeys (method, path){
var prefix = '[Express Route Fake Error] ';
var methodMessage = 'Method \'' + method + '\' is not supported';
var pathMessage = 'Action for method \'' + method + '\' with route \'' + path + '\' does not exist';
throwOnUndefined(routeFunctions[method], prefix + methodMessage);
throwOnUndefined(routeFunctions[method][path], prefix + pathMessage);
}
function getRouteAction(method, path) {
throwOnBadKeys(method, path);
return routeFunctions[method][path];
}
function reset() {
routeFunctions.delete = {};
routeFunctions.get = {};
routeFunctions.post = {};
routeFunctions.put = {};
}
function Router() {
return {
delete: routeStub.bind(null, 'delete'),
get: routeStub.bind(null, 'get'),
post: routeStub.bind(null, 'post'),
put: routeStub.bind(null, 'put')
};
}
module.exports = {
Router: Router,
getRouteAction: getRouteAction,
reset: reset
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment