Search code examples
kitura

How to pass computed result to multiple Kitura handler block?


As we can call multiple handler from router get request, how do we pass, say the result computed in the first handler to the next handler block ?

// Uses multiple handler blocks
    router.get("/multi", handler: { request, response, next in
        response.send("I'm here!\n")
        next()
        }, { request, response, next in
            response.send("Me too!\n")
            next()
    })

Solution

  • You can use the userInfo dictionary to hold computed data in the request object, and it'll be accessible in the subsequent handlers.

    https://ibm-swift.github.io/Kitura/Classes/RouterRequest.html#/s:vC6Kitura13RouterRequest8userInfoGVs10DictionarySSP__