Action
Examples using Default and Custom Handlers
Using and Extending Suvidha’s Default Handlers
Suvidha’s default handlers offer a quick start for request/response management. They handle common tasks and can be easily extended.
Introduction
Default handlers provide a pre-configured setup. They handle response formatting, errors, and successful requests, allowing you to focus on your logic. They’re ready to use yet customizable.
Basic Usage
Default handlers are created with new DefaultHandlers()
. suvidha()
uses them to process requests. Your handler function contains your logic. Return data, use Http
classes, or plain Protocol
objects for responses.
Extending
Extend the default handlers to customize behavior. Create a class that inherits from DefaultHandlers
and overrides its methods.
User Profile Endpoint with SimpleCustomHandlers
Customization
Logging
Override onComplete
or onErr
.
Setting Headers
Override onComplete
or onErr
.
Using next(err)
(Within Suvidha Handlers)
Suvidha handlers receive the next
function, allowing you to pass errors to Express.js error handling middleware or the default Express.js error handler. This is particularly useful if you have existing error handling logic in Express.js that you want to leverage.
Custom Error Handling
Override onErr
.
Modifying Response Format
Override the formatter.
Middleware Integration
Use Suvidha’s .use()
method for middleware, which preserves type safety and allows context propagation.