Last active
March 31, 2017 14:53
-
-
Save adamauckland/586cb326d2fd010250482bb0d34cde3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"path": "/todo/list", | |
"handler": "ListHandler", | |
"verb": "get" | |
}, | |
{ | |
"path": "/todo/list/:id", | |
"handler": "DetailHandler", | |
"verb": "get" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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