Skip to content

Instantly share code, notes, and snippets.

@grncdr
Created October 23, 2011 02:29
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 grncdr/1306770 to your computer and use it in GitHub Desktop.
Save grncdr/1306770 to your computer and use it in GitHub Desktop.
Generate a set of URI Templates from the routes in an express app
makeTemplate = (path) ->
i = 0
path.replace(/\*(\w)?/g, "{+raw#{i++}}$1")
.replace(/:(\w+)/g, '{$1}')
.replace(/List}/g, 's*}')
uriTemplateMiddleware = (path='/') ->
spec = null
(req, res, next) ->
if req.path == path
spec ?= (makeTemplate(r.path) for r in req.app.routes.all())
return res.send spec
next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment