dongxian6285 2016-01-06 17:34
浏览 14
已采纳

马提尼酒中的处理机叫什么顺序?

About golang martini

  • We can add middlewares using m.Use(). Of course, "Middleware Handlers are invoked in the order that they are added".
  • In addition, a handler can also be added by router like r.Get("/", handler).
  • Sometimes, we also need a handler be called after the router handler. That is a handler is called before something is written to ResponseWriter.

So, how to order of presentation of these handlers? I can not get solution is martini's document.

  • 写回答

1条回答 默认 最新

  • duanqun7761 2016-01-07 09:24
    关注

    As you said, middlewares in Martini and others are called in the order they are defined: first the ones added with use, then the route middlewares, then the route handler.

    Here is the middleware example given by the martini documentation:

    // log before and after a request
    m.Use(func(c martini.Context, log *log.Logger){
        log.Println("before a request")
    
        c.Next()
    
        log.Println("after a request")
    })
    

    According to this, if you have middlewares A and B and the route R, then the call chain will be something like that:

    func A() {
        // Do things before B
    
        func B() {
            // Do things before R
    
            func R() {
                // Do things in R
            } ()
    
            // Do things after R
        }()
    
        // Do things after B
    }
    

    So depending of what you need, you will need to add code in a middleware before or after the Next() call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题