Mock Server File Organization
Code-based mock servers support a modular approach to organizing your scripts, similar to professional development practices. This structure promotes cleaner code, better maintainability, and easier reuse of components across your mock API.
All code-based mock files reside in the /apigit-mock directory of your API Repository, which serves as the root for all mock server scripts.
💡
Important: The
/apigit-mock/main.js file must exist as it serves as the entry point loaded by the APIGIT underlying MockServer framework.Recommended Structure
Organize your mock server files with a clear separation of concerns:
- main.js: Define routes and import handler modules
- handlers/: Directory containing specialized request handlers
- models/: (Optional) Data models and schemas
- utils/: (Optional) Helper functions and utilities
Example Implementation
main.js - Route definitions importing handler modules:
// Import handler modules at the top for clarity
const { userPostHandler, userDeleteHandler } = require('./handlers/userHandlers');
const { productListHandler } = require('./handlers/productHandlers');
// Define routes with their corresponding handlers
mock.post('/user/:name', userPostHandler);
mock.delete('/user/:name', userDeleteHandler);
mock.get('/products', productListHandler);handlers/userHandlers.js - Specialized user operation handlers:
// User-related request handlers
function userPostHandler(req, res) {
// Implementation for creating users
res.status(201).send({ message: 'User created successfully' });
}
function userDeleteHandler(req, res) {
// Implementation for deleting users
res.status(200).send({ message: 'User deleted successfully' });
}
// Export functions as a module
module.exports = {
userPostHandler,
userDeleteHandler
};This modular approach enables you to:
- Group related functionality logically
- Reuse handlers across different routes
- Maintain a clean separation between route definitions and implementation logic
- Scale your mock API more effectively as complexity grows
Last updated on