A utility package for Express.js applications offering simplified async handling, error responses, validation, rate limiting, and logging.
Grom is a powerful utility package designed to enhance Express.js applications by providing a standardized approach to error handling, input validation, rate limiting, structured responses, and logging. It simplifies backend development by enforcing best practices and reducing boilerplate code.
Developers often face repetitive tasks in Express.js applications, such as:
- Handling async errors properly.
- Validating user input against expected schemas.
- Preventing API abuse with rate limiting.
- Standardizing API responses.
- Implementing structured logging.
Grom solves these problems by offering a robust, modular utility class that integrates seamlessly into Express.js applications.
npm install grom-utils
- Async Handler: Wrap async functions to catch errors.
- Error Checker: Throw custom API errors with ease.
- API Responses: Standard and paginated API responses.
- Input Validation: Schema-based input validation.
- Rate Limiting: Simple middleware to limit requests per IP.
- Logging: Colorful, timestamped logs.
Problem: Handling errors in async Express.js routes requires repetitive try-catch blocks.
Solution: Grom wraps async functions to automatically catch errors and forward them to Express middleware.
Example:
import { grAsyncHandler } from "grom-utils";
app.get("/data", grAsyncHandler(async (req, res) => {
const data = await fetchData(); // Assume async function
res.json({ success: true, data });
}));
Problem: Manually checking request payloads for type mismatches is tedious and error-prone.
Solution: Grom validates data against a predefined schema and throws an error if mismatched.
Example:
import { grValidate } from "grom-utils";
const userSchema = { name: "string", age: "number" };
app.post("/register", (req, res) => {
grValidate(userSchema, req.body);
res.json({ message: "User registered successfully" });
});
Problem: API responses often lack consistency in structure and formatting.
Solution: Grom provides a standard response format with status codes, messages, and metadata.
Example:
import { grResponse } from "grom-utils";
app.get("/status", (req, res) => {
res.json(grResponse(200, { status: "OK" }, "Service is running"));
});
Problem: Developers often need to enforce custom conditions before proceeding.
Solution: Grom throws an API error if a condition is false.
Example:
import { grCheck } from "grom-utils";
app.post("/order", (req, res) => {
grCheck(req.body.items.length > 0, 400, "Order cannot be empty");
res.json({ message: "Order placed successfully" });
});
Problem: APIs are vulnerable to abuse through excessive requests.
Solution: Grom limits requests per IP address within a specified time window.
Example:
import { grRateLimit } from "grom-utils";
app.use(grRateLimit(5, 10000)); // Max 5 requests per 10 sec
Problem: Logs are often unstructured, making debugging difficult.
Solution: Grom provides color-coded logs with timestamps.
Example:
import { grLogger } from "grom-utils";
grLogger("info", "Server started on port 3000");
grAsyncHandler
grCheck
grResponse
grValidate
grRateLimit
grLogger
- ✅ Reduces repetitive boilerplate code.
- ✅ Improves API security and reliability.
- ✅ Enforces best practices in Express.js.
- ✅ Easy integration into existing projects.
MIT License
For issues and feature requests, open an issue on GitHub.
Dhruv Sharma ([@dhruv-sharma007])
Jasraj Chouhan ([@Jasraj Chouhan])
Become contributor in this project