Skip to content

Instantly share code, notes, and snippets.

@adamauckland
Last active March 31, 2017 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamauckland/586cb326d2fd010250482bb0d34cde3a to your computer and use it in GitHub Desktop.
Save adamauckland/586cb326d2fd010250482bb0d34cde3a to your computer and use it in GitHub Desktop.
[
{
"path": "/todo/list",
"handler": "ListHandler",
"verb": "get"
},
{
"path": "/todo/list/:id",
"handler": "DetailHandler",
"verb": "get"
}
]
var express = require('express');
var app = express();
// load config from file
var config = require('config.json');
// handler mappings
var handlers = {
'ListHandler': require('list-handler'), // different request handlers in different modules
'DetailHandler': require('detail-handler')
};
config.forEach((route) => {
// lookup the request handler from the handler mapping above so different paths can call different handlers.
let routeHandler = handlers[route.handler];
// now set up a route at the path defined in the config pointing to the handler
app[route.verb](route.path, routeHandler);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment