Requests are handled asynchronously, stepping down the stack until complete.
let request = { id: 1, jsonrpc: '2.0', method: 'hello' }engine.handle(request, function(err, response){ // Do something with response.result, or handle response.error})// There is also a Promise signatureconst response = await engine.handle(request)
Middleware have direct access to the request and response objects.
They can let processing continue down the stack with next(), or complete the request with end().
async middleware do not take an end callback.
Instead, the request ends if the middleware returns without calling next():
engine.push(createAsyncMiddleware(async (req, res, next) => { res.result = 42 /* The request will end when this returns */}))
The next callback of async middleware also don't take return handlers.
Instead, you can await next().
When the execution of the middleware resumes, you can work with the response again.
engine.push(createAsyncMiddleware(async (req, res, next) => { res.result = 42 await next() /* Your return handler logic goes here */ addToMetrics(res)}))
You can freely mix callback-based and async middleware: