Created
September 11, 2012 04:21
-
-
Save balupton/3695936 to your computer and use it in GitHub Desktop.
DocPad: Custom Routing
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
# ================================= | |
# DocPad Events | |
# Here we can define handlers for events that DocPad fires | |
# You can find a full listing of events on the DocPad Wiki | |
events: | |
# 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 | |
require(__dirname+'/routes.coffee')({docpad,sever}) |
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
# Our custom routes for our DocPad Server | |
# Loaded via our require in the serverExtend event in our docpad.coffee 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 = config.templateData.site.oldUrls or [] | |
newUrl = config.templateData.site.url | |
# Redirect any requests accessing one of our sites oldUrls to the new site url | |
server.use (req,res,next) -> | |
if req.headers.host in oldUrls | |
res.redirect(newUrl+req.url, 301) | |
else | |
next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.