Express-Hooked is a powerful extension for Express.js that brings a comprehensive hooks and filters system to your applications. Inspired by WordPress hooks system, it allows developers to extend functionality at various points in the request lifecycle without modifying core code.
- Flexible Architecture: Hook into request processing at multiple stages
- Non-Intrusive: Extend functionality without modifying existing code
- Priority System: Control execution order with priority levels
- Namespace Support: Organize hooks with namespaces for better maintainability
- Easy Integration: Simple setup with minimal configuration
npm install express-hookedconst express = require('express');
const ExpressHooked = require('express-hooked');
const app = express();
const port = 3000;
// Initialize ExpressHooked
const expressHooks = new ExpressHooked(app);
// Register an action hook
expressHooks.addAction('request_started', (req, res, next) => {
console.log(`Request received: ${req.method} ${req.url}`);
next();
});
// Register a filter hook
expressHooks.addFilter('modify_response_data', (data) => {
return {
...data,
processedBy: 'ExpressHooked',
timestamp: new Date().toISOString()
};
});
// Example route using filters
app.get('/', (req, res) => {
let responseData = { message: 'Hello World!' };
responseData = expressHooks.applyFilters('modify_response_data', responseData);
res.json(responseData);
});
// Initialize hooks integration
expressHooks.init();
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});Actions allow you to execute code at specific points without modifying data:
expressHooks.addAction('request_started', (req, res, next) => {
// Do something when request starts
next();
});Filters allow you to modify data flowing through your application:
expressHooks.addFilter('modify_response_data', (data) => {
// Modify and return the data
return { ...data, customField: 'value' };
});Control execution order with priority levels (lower numbers execute first):
// This executes first (higher priority)
expressHooks.addAction('request_started', callback, 5);
// This executes later (lower priority)
expressHooks.addAction('request_started', callback, 15);Organize your hooks with namespaces:
// Register hook with namespace
expressHooks.addAction('myplugin::before_request', callback);
// Execute namespaced hook
expressHooks.doAction('myplugin::before_request', req, res);- Authentication & Authorization: Hook into request processing to validate credentials
- Logging & Analytics: Track requests, responses, and performance metrics
- Caching: Implement caching strategies at different levels
- API Transformation: Modify API responses before sending to clients
- Error Handling: Centralized error handling with custom logic
- Rate Limiting: Implement rate limiting based on custom criteria
We welcome contributions! Please see our contributing guidelines for more information.
Apache 2.0
Repository: https://gitlab.com/bytedogssyndicate1/express-hooked