Does using the before hook for a module, apply to server run code as well?

I just wanted to double check this. If we create a lua module to be triggered by a before hook, that’s only when the message is coming from the client correct? So we only read storage objects one in our application on init, but we are reading and writing in all kinds of ways via server code. We wouldn’t want our module being called by nk.storage_read.

@oscargoldman Yes. A before or after hook register a custom function to be called before or after the client request is passed to one of the core functions in the server to be processed.


local function before_get_account(context, payload)
    local input = nk.json_encode(payload)
    nk.logger_info(("get account request: %q"):format(input))
    -- must return payload to be used by the get account core function
    return payload
nk.register_req_before(before_get_account, "GetAccount")

This example will print the contents of the request input to the logs before the core function for get account is executed in the server. You can think of the server hooks as lifecycle events in a request pipeline.

client request -> before hook -> request core function -> client response -> after hook

If the core functions are called directly within an RPC function they will not trigger the before or after hook in the request pipeline.


local account = nk.account_get_id("8f4d52c7-bf28-4fcf-8af2-1d4fcf685592")
local output = nk.json_encode(account)
nk.logger_info(("user account: %q"):format(output))