What is it?
Express-Octopus is a fast and a lightweight Express Auto-Router which loads your endpoints (routes) from the file system automatically.
It is made with the simplicity and scalability in mind, so it is suitable both for small and large applications.
Endpoints (Routes) are loaded in a straightforward way, your directories and file names become path in your endpoint (route).
For example, for the following directory structure:
endpoints/
users/
all/
index.js
We will get the following path: /users/all/
As you can see, the index.js is ommited from the path, so if you specify, for example, a.js, the path will be: /users/all/a
The directory endpoints/ is a base directory that you specify, so it is ommited from the path.
Endpoints (Routes)
Endpoints (Routes) must be witten in a form of a CommonJS module, which exports an ES6 class which extends the OctopusEndPoint class.
NOTE: You must not instantiate the class, it must be directly exported without new keyword! Also, you must use ES6 class, since the module is written to check the class with regex, (If you need/prefere ES5 prototype support, feel free to contribute).
Endpoint (Route) class format
Your endpoint (route) class, always needs to have constuctor and super constructor called inside.
For each HTTP Request method, you need to specify the method in the class.
Method format:
async? <http method name>request response /* method body */
Rules for HTTP Request Method methods:
- method name must be just HTTP Request method (Currently supported: get, post, put, patch, delete)
- method name must be lowercase
- method always has 2 parameters (no less, no more) (Those are: request, response)
- async is not requiered, it is optional (recommended to use)
Optinally you can handle middleware for each HTTP Request Method.
For each HTTP Request method middleware, you need to specify the method in the class.
Middleware Method format:
async? <http method name> { /* method body */}
Rules for middleware methods:
- method name must have HTTP Request method (Currently supported: get, post, put, patch, delete)
- method name must be lowercase
- method always has 3 parameters (no less, no more) (Those are: request, response, next)
- async is not requiered, it is optional (recommended to use)
Using parameters
If you want to parameters in the URL, you can pass a string with them inside the super in constuctor.
Example:
{ super':id';}
You can use this to extend the url path more.
Example:
{ super'more/:id/:anotherparam/other';}
Examples
Default development configuration:
// Dependenciesconst ExpressOctopus = ;const Express = ;const App = ; // Setup with Default Development configuration (Cache is disabled and debug messages are printed)const ExpressOctopusCore = App ExpressOctopusDefaultConfigurationDevelopment; // AutowiringExpressOctopusCore; // Start the Express server on the port 3000App;
Default production configuration:
// Dependenciesconst ExpressOctopus = ;const Express = ;const App = ; // Setup with Default Production configuration (Cache is enabled and debug messages are not printed)const ExpressOctopusCore = App ExpressOctopusDefaultConfigurationProduction; // AutowiringExpressOctopusCore; // Start the Express server on the port 3000App;
Custom configuration:
// Dependenciesconst ExpressOctopus = ;const Express = ;const Path = ;const App = ; // Setupconst ExpressOctopusCore = // Express instance (Aka: Express()) App // Configuration // Base Directory where you create your end points path: Path // Development mode (Disables cache) // Recommended for development, since you would need to manually delete cache on each run // Optional field developer: true // Prints debug messages // Recommended for development, helps a lot // Optional field // Independent of a developer field printMessages: true // Caching configuration cache: // Is the cache enabled? // Requiered field, must be of a boolean type (true/false) enabled: true // Directory where the cache is saved // Requiered field path: Path // Expiration of the cache in days // Requiered field expiration: 30 ; // AutowiringExpressOctopusCore; // Start the Express server on the port 3000App;
Simple Endpoint (Route):
const OctopusEndPoint = OctopusEndPoint; // You must extend upon OctopusEndPoint class! { // It is requiered to call super in the constructor // Argument is the parameters from the url // Example: super(':id'); super""; } // Handle the middleware for the GET request // Always has 3 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method>_middleware // Method name needs to be lowercase! async { console; ; } // Handle the middleware for the POST request // Always has 3 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method>_middleware // Method name needs to be lowercase! async { console; ; } // Handle the middleware for the PUT request // Always has 3 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method>_middleware // Method name needs to be lowercase! async { console; ; } // Handle the middleware for the PATCH request // Always has 3 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method>_middleware // Method name needs to be lowercase! async { console; ; } // Handle the middleware for the DELETE request // Always has 3 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method>_middleware // Method name needs to be lowercase! async { console; ; } // Handle the GET request // Always has 2 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method> // Method name needs to be lowercase! async { console; response; } // Handle the POST request // Always has 2 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method> // Method name needs to be lowercase! async { console; response; } // Handle the PUT request // Always has 2 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method> // Method name needs to be lowercase! async { console; response; } // Handle the PATCH request // Always has 2 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method> // Method name needs to be lowercase! async { console; response; } // Handle the DELETE request // Always has 2 parameters, no more, no less // Async is not requiered, but recommended // Method name needs to be in the following format: <HTTP request method> // Method name needs to be lowercase! async { console; response; } // Must be class reference directly, do not instantiate class!moduleexports = SimpleEndPoint;
Simple Endpoint (Route) - Real world Example:
const OctopusEndPoint = OctopusEndPoint;const UserContoller = ; { super":id"; } async { UserContoller; } async { UserContoller; } moduleexports = DeleteUser;
TODO:
- Validate URL parameters with Regex
Contributing:
If you have any suggestions, ideas or you have found a bug, feel free to contribute :)
Github Repository: