Utils and Helpers in Express
Utils (Utility) aur Helpers folder aapke codebase mein ek tarah se aise functions ya modules rakhte hain jo reusable hote hain aur kai…
Utils and Helpers in Express JS
Utils (Utility) aur Helpers folder aapke codebase mein ek tarah se aise functions ya modules rakhte hain jo reusable hote hain aur kai jagah pe use ho sakte hain. In folders ka main purpose code ko modular aur maintainable rakhna hota hai.
1. Utils Folder:
Is folder mein aise small utility functions hote hain jo specific tasks ko perform karte hain. Jaise kisi data ko format karna, date ko manipulate karna, kisi string ko sanitize karna, ya kisi calculation ko perform karna.
Example:
Maan lo, humein kai jagah pe date ko format karna hai.
// utils/dateUtils.js
function formatDate(date) {
const d = new Date(date);
return `${d.getDate()}/${d.getMonth() + 1}/${d.getFullYear()}`;
}
module.exports = { formatDate };
Ab agar humein kahin bhi date ko format karna ho, toh hum har baar same code likhne ki bajaye bas formatDate function ko call kar sakte hain:
// main.js
const { formatDate } = require('./utils/dateUtils');
console.log(formatDate('2025-02-18')); // Output: 18/2/2025
Isse code reuse hota hai aur agar kabhi date formatting mein change karna ho, toh ek jagah se kar sakte hain.
2. Helpers Folder:
Helpers folder mein bhi similar reusable code hota hai, lekin yeh thoda general-purpose hota hai. Yeh functions aapke application ke specific logic mein help karte hain.
Example:
Maan lo aapke paas ek shopping cart hai aur aapko discount apply karna hai. Helper function is task ko simplify kar sakta hai.
// helpers/discountHelper.js
function applyDiscount(price, discount) {
return price - (price * (discount / 100));
}
module.exports = { applyDiscount };
Isko aise use kiya jaa sakta hai:
// main.js
const { applyDiscount } = require('./helpers/discountHelper');
const discountedPrice = applyDiscount(1000, 20);
console.log(discountedPrice); // Output: 800
Main Difference:
- Utils zyada low-level utility functions ko represent karte hain, jo specific tasks jaise formatting ya calculations ke liye hote hain.
- Helpers thoda higher-level tasks ko simplify karte hain, jo aapke business logic ya application ke specific flow se related hote hain.
Kyun Use Karte Hai?
- Code Reusability: Aap same code ko baar-baar likhne ki bajaye, ek function ko call kar ke kaam kar sakte hain.
- Maintainability: Agar kisi function mein koi change karna ho, toh aapko sirf ek jagah change karna padta hai, puri application mein nahi.
- Clean Code: Aapke codebase mein logic clear aur simple hota hai. Chhote chhote reusable functions ko alag se rakhke, aapke main code ko clutter se bachaya jaata hai.
In short, utils aur helpers folders aapke project ko clean, maintainable, aur easily extendable banane mein help karte hain.
메타데이터
- post_id
- d896d76763ee
- slug
- utils-and-helpers-in-express-d896d76763ee
- url
- https://medium.com/@rk85783/utils-and-helpers-in-express-d896d76763ee
- canonical_url
- https://medium.com/@rk85783/utils-and-helpers-in-express-d896d76763ee
- author_url
- https://medium.com/@rk85783
- status
- ok
- fetched_at
- 2026-06-26 08:21:59