Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
DocPad: Custom Routing
# =================================
# DocPad Events
# Here we can define handlers for events that DocPad fires
# You can find a full listing of events on the DocPad Wiki
# Server Extend
# Used to add our own custom routes to the server before the docpad routes are added
serverExtend: (opts) ->
# Extract the server from the options
{server} = opts
docpad = @docpad
# Include our custom routes
# Our custom routes for our DocPad Server
# Loaded via our require in the serverExtend event in our configuration file
module.exports = ({server,docpad}) ->
# As we are now running in an event,
# ensure we are using the latest copy of the docpad configuraiton
# and fetch our urls from it
config = docpad.getConfig()
oldUrls = or []
newUrl =
# Redirect any requests accessing one of our sites oldUrls to the new site url
server.use (req,res,next) ->
if in oldUrls
res.redirect(newUrl+req.url, 301)

This comment has been minimized.

Copy link

@mklement0 mklement0 commented Aug 19, 2013

Cool; note that there's a typo in the last line of sever should be server.


This comment has been minimized.

Copy link

@Gems Gems commented Oct 12, 2013

At the first file you have "routes" module initialization through "require" function that you've passed two arguments in the following order — docpad, server

But at the second file you've defined module that expects parameters in reverse order — server, docpad.

May be I missed something but it is looks like a bug.


This comment has been minimized.

Copy link

@davidgtonge davidgtonge commented Feb 18, 2014

@Gems - thats the advantage of coffee-script object creation / de-structuring. The order doesn't matter. Its just shorthand for creating an object and extracting the values from that object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment