简体   繁体   English

如何在Docpad中处理路线

[英]how to handle routes in Docpad

This should be really obvious but I just cant get my head around it 这应该是很明显的,但是我无法理解

How do I add extra routes in Docpad?? 如何在Docpad中添加额外的路由?

Im looking for the Docpad equivalent to express.js's 我正在寻找等效于express.js的Docpad

app.post("*", function(res,req,next){
//Do stuff
}

As far as I can understand I need to create a plugin module for this? 据我了解,我需要为此创建一个插件模块吗? How do I tell Docpad to use my routes? 如何告诉Docpad使用我的路线? Im guessing it has something to do with the extend-server event, do I put that as parameter in docpad.coffee? 我想这与扩展服务器事件有关,我是否将其作为参数放入docpad.coffee?

How do I pass the req object to my route handler? 如何将req对象传递给路由处理程序?

can I force docpad to always consider my routing first? 我可以强制docpad始终首先考虑我的路由吗? kinda like middleware? 有点像中间件? can I pass a (processed) url back to docpads standard routing? 如何将(处理后的)URL传递回docpads标准路由? how? 怎么样?

Are you looking for something like this: 您是否正在寻找这样的东西:

server.get /list\/[a-zA-Z]+/, (req,res,next) ->
                document = docpad.getCollection('documents').findOne({relativeOutPath: 'index.html'});
                docpad.serveDocument({
                    document: document,
                    req: req,
                    res: res,
                    next: next,
                    statusCode: 200
                });

This is an event (server extend) in the docpad.coffee file. 这是docpad.coffee文件中的事件(服务器扩展)。 Its intercepting the request and testing it against a regex (could easily just be a plain url). 它拦截请求并针对正则表达式对其进行测试(可以很容易地成为纯URL)。 The user will see the url they input but the index.html will be served. 用户将看到他们输入的URL,但将提供index.html。

Or closer to your case: 或更接近您的情况:

server.post "*", (req,res,next) ->
                #do stuff

inside docpad.coffee 里面docpad.coffee

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

        # 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
        latestConfig = docpad.getConfig()
        oldUrls = latestConfig.templateData.site.oldUrls or []
        newUrl = latestConfig.templateData.site.url

        server.post "*", (req,res,next) ->
          #do stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM