A powerful response management package for Node.js, featuring multi-language support, dynamic response codes, and high-performance storage with Redis & SQLite.
π Automatic Response Formatting β Simplifies handling success and error responses.
π Multi-Language Support β Fetches localized messages from Redis & SQLite.
π Dynamic Storage β Uses Redis for caching, with SQLite as a fallback.
π Optimized for Scalability β Works in both single-server and distributed environments.
π Fully Configurable β Manage response codes, messages, and settings via config.yml
.
git clone https://github.com/YOUR_GITHUB_USERNAME/krh-node.git
cd krh-node
This automatically creates the project structure, installs dependencies, and initializes databases.
npm run dev
or for production:
npm run build && npm start
All settings are stored in config/config.yml
:
supportedLanguages: ['en', 'fr']
codeCategories:
auth: 200100
payment: 200200
validation: 400100
storage:
sqlite:
path: './storage-data/messages.db'
redis:
host: 'localhost'
port: 6379
Modify .env
for custom Redis & SQLite settings:
REDIS_HOST=localhost
REDIS_PORT=6379
SQLITE_DB_PATH=./storage-data/messages.db
Add the middleware to your Express app:
import express from 'express';
import { responseManager } from './src/middleware/responseHandler';
const app = express();
app.use(responseManager());
app.get('/user', (req, res) => {
res.success(200101, { user: 'Alice' });
});
app.get('/error', (req, res) => {
res.error(400101, "Invalid request");
});
app.listen(3000, () => console.log('Server running on port 3000'));
import { messageStorage } from './src/storage';
async function testStorage() {
await messageStorage.setMessage(200101, 'en', 'User retrieved successfully');
const msg = await messageStorage.getMessage(200101, 'en');
console.log(msg); // Output: User retrieved successfully
}
testStorage();
π Automatically sets HTTP status (based on code).
π Fetches localized message if no customMessage
is provided.
π Wraps response in a standard format.
res.success(200101, { user: 'Alice' });
// Response:
{
"status": "success",
"code": 200101,
"message": "User retrieved successfully",
"data": { "user": "Alice" }
}
π¨ Standardized error handling.
π¨ Automatically maps HTTP status from code.
π¨ Includes error details for debugging.
res.error(400101, "Invalid input");
// Response:
{
"status": "error",
"code": 400101,
"message": "Validation failed. Please check your input.",
"error": "Invalid input"
}
krh-node/
βββ config/
β βββ config.yml # Package-wide configuration
βββ src/
β βββ bootstrap/ # Handles DB & Redis connections
β βββ config/ # Loads configuration settings
β βββ core/ # Core logic (Code Registry, Localization, Errors)
β βββ middleware/ # Express middleware
β βββ services/ # Business logic for response handling
β βββ storage/ # SQLite & Redis storage providers
β βββ types/ # TypeScript interfaces
β βββ utils/ # Helpers (logging, validation, etc.)
β βββ index.ts # Main entry file
βββ storage-data/ # SQLite database & migrations
βββ tests/ # Unit & Integration tests