Skip to content

Instantly share code, notes, and snippets.

@domachine
Created August 20, 2016 00:09
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 domachine/a89510f9ca025878d9edeb918abf33dd to your computer and use it in GitHub Desktop.
Save domachine/a89510f9ca025878d9edeb918abf33dd to your computer and use it in GitHub Desktop.
const routes = [
{ path: '/api/v1/websites/:website', method: 'GET', service: getWebsite },
{ path: '/api/v1/websites/:website', method: 'POST', service: createWebsite }
]
const notFound = (req, res) => res.end('Not found')
const api = proxyHandler(routes, { fallback: notFound })
http.createServer(api).listen(3000)
function proxyHandler (routes, { fallback }) {
const compiledRoutes = map(
r => Object.assign({}, r, { path: pathToRegexp() }),
routes
)
return (req, res) => {
const route = compose(
r => r || fallback,
find(r => r.method === req.method),
filter(r => req.url.match(r.path))
)(compiledRoutes)
route(req, res)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment