Plugins
The feTS Server includes an extension system, offering the ability to interact with the request/response process.
onRequest
- Invoked prior to the router handling the request.- It features an
endResponse
function that takes aResponse
object to preemptively conclude the request.
- It features an
onResponse
- Invoked following the router’s handling of the request.- It offers the ability to alter the response before it’s delivered to the client.
onRouteHandle
- When a route is matched, this is invoked with all the details of the route. So you can apply any logic (tracing, auth etc) before the actual handler is invoked.
Recording the Response Delay
We’ll develop an extension that determines the response delay and logs it in the console.
import { RouterPlugin } from 'fets'
export function useLogDelay(): RouterPlugin {
const initialTimePerRequest = new WeakMap<Request, number>()
return {
onRequest({ request }) {
initialTimePerRequest.set(request, Date.now())
},
onResponse({ request, response }) {
const initialTime = initialTimePerRequest.get(request)
if (initialTime) {
const delay = Date.now() - initialTime
console.log(`Response delay: ${delay}ms`)
}
}
}
}