1.0.0 • Published 4 months ago
flow-server-plugin-route-class-method v1.0.0
Route Class Method Plugin for Flow Server Framework
This plugin provides a class-based routing system for the Flow Server Framework, allowing you to define routes using controller classes and methods.
Features
- Define routes using controller classes and methods
- Automatic route registration based on class structure
- Support for RESTful routes
- Route parameters
- HTTP method mapping from method names
- Customizable route prefixes
Installation
- Clone the repository:
git clone /new-bare3/flow-server-plugin-routeClassMethod.git
- Install dependencies:
cd flow-server-plugin-routeClassMethod
npm install
Configuration
Add the following to your Flow Server Framework configuration file (config.json
):
{
"plugins": {
"routeClassMethod": {
"controllersPath": "controllers",
"routePrefix": "/api"
}
}
}
Usage
Creating Controllers
Create controller classes in your controllers directory:
// controllers/UserController.js
class UserController {
// GET /api/user
index(req, res) {
return {
users: [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Jane Smith' }
]
};
}
// GET /api/user/by-id
getById(req, res) {
const userId = req.params.id;
return { user: { id: userId, name: `User ${userId}` } };
}
// POST /api/user/create
postCreate(req, res) {
const userData = req.body;
return { success: true, user: { id: 3, ...userData } };
}
// PUT /api/user/update
putUpdate(req, res) {
const userId = req.params.id;
const userData = req.body;
return { success: true, user: { id: userId, ...userData } };
}
// DELETE /api/user/remove
deleteRemove(req, res) {
const userId = req.params.id;
return { success: true, message: `User ${userId} deleted` };
}
}
module.exports = UserController;
Method Naming Conventions
Method names are automatically mapped to HTTP methods and routes:
index
→GET /controller
getUsers
→GET /controller/users
postCreate
→POST /controller/create
putUpdate
→PUT /controller/update
deleteUser
→DELETE /controller/user
Methods that start with an underscore (e.g., _privateMethod
) are considered private and will not be registered as routes.
Route Parameters
You can define route parameters in your paths:
// controllers/ProductController.js
class ProductController {
// GET /api/product/:id
getById(req, res) {
const productId = req.params.id;
return { product: { id: productId, name: `Product ${productId}` } };
}
}
Manual Registration
You can also register controllers and routes manually:
const plugin = flow.services.get('routeClassMethod');
// Register a controller
const MyController = require('./MyController');
plugin.register('custom', new MyController());
// Add a route
plugin.addRoute('GET', '/custom-route', controller, 'methodName');
API Reference
RouteClassMethodPlugin
init(flow)
: Initialize the pluginregister(name, Controller)
: Register a controlleraddRoute(method, path, controller, methodName)
: Add a route manually
Testing
Run the test script to verify the plugin is working correctly:
npm test
License
MIT
1.0.0
4 months ago