Custom HTTP Runtime
You can integrate Pikku into most javascript HTTP servers in three steps.
- Create a bridge for the
request
- Create a bridge for the
response
- Call
runHTTPRoute
from your runtimes event
info
We'll use express here as the example request/response as it's most popular.
Create a class to extend PikkuRequest
This wraps the request object provided into a pikku request object.
Pikku Express Request
loading...
Create a class to extend PikkuResponse
This wraps the response object provided into a pikku response object.
Pikku Express Response
loading...
Call runHTTPRoute with the correct request and response object
The runRoute
method will find the correct route to run, and check permissions / authentication, validate data against the schema and map any errors to their errors.
Pikku Middleware
await runHTTPRoute(
new PikkuExpressRequest(req),
new PikkuExpressResponse(res),
singletonServices,
createSessionServices,
{
// The HTTP method
method: req.method.toLowerCase() as any,
// The HTTP Route
route: req.path,
// Whether we want allow the route handler to return a 404, or
// not do anything if the route isn't found
respondWith404: true
// This skips us trying to find a session for a route that
// isn't authenticated
skipUserSession: false
}
)
Wiring this with express
To get an idea how this all ties together, let's now look at the express middleware plugin.
Pikku Express Middleware
loading...