basic-controller
Basic implemetation of controllers for NodeJS
Installation
npm install basic-controller --save
Usage
Include
var Controller = ;
new Controller(controllerFilepath);
Create a instance of a controller.
Arguments
Name | Type | Description |
---|---|---|
controllerFilepath |
string |
Controller file path. |
Example
Controller File
// controllers/main/mainContoller.js { // Make something cool } { // Make something else cool }moduleexports = MainCtrl;
// controllers/main/otherContoller.jsmodule {}; OtherCtrlprototype { // Make great things};
Controller instance
const MainController = 'controllers/main/mainContoller.js';const OtherController = 'controllers/main/otherContoller.js';
controller.link(methodName);
Make a request handler to link a route with a controller method.
Arguments
Name | Type | Description |
---|---|---|
methodName |
string |
Method name |
Example
// controllers/mycontroller.js { // Make something cool }moduleexports = MainCtrl;
const app = ;const MyCtrl = 'controllers/mycontroller.js';app;
Reciving params
Each method receives as arguments the parameters in the route, followed by an object with these same parameters, the request and the response. example:
Example
// controllers/mycontroller.js { // Request: GET /myRoute // params: { } } { // Request: GET /anotherRoute/alexander/bolivar // displayName: 'alexander' // province: 'bolivar' // params: { displayName: 'alexander', province: 'bolivar' } }moduleexports = MainCtrl;
const app = ;const MyCtrl = 'controllers/mycontroller.js';app;app;
Render view
When a request is handler through a controller method, the response will be the
rendering of a view with the same name of the method in the views
folder where
the controller is if this view exists. The view is rendered with params returned
in the called method.
Example
// controllers/mycontroller.js { return displayName: 'Alexander' ; }moduleexports = MainCtrl;
<!-- controllers/views/myMethod.ejs --><%= displayName %> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat facilis, ipsum laborum possimus. Assumenda, vitae repellat. Laudantium eos beatae, repellendus provident. Nam error voluptatem labore amet sit. Odio, explicabo, illum!
const app = ;const MyCtrl = 'controllers/mycontroller.js';// Render view controllers/views/myMethod.ejsapp;app; // Configure view engine
controller.setView(view=null)
Set a view to render.
Arguments
Name | Type | Description |
---|---|---|
view |
string |
View to render |
Example
// controllers/mycontroller.js { this; // render controllers/views/anotherView.ejs }
controller.render(view=null, locals = {})
To force render with render
method. Is the action method
return with render
method call. Also in this case, is view doesn't exists
an error will throwed.
Arguments
Name | Type | Description |
---|---|---|
view |
string |
View to render |
locals |
object |
Locals to render view |
Example
// controllers/mycontroller.js { // Render 'anotherView' view. return this; } { // Same view 'amazingMethod' with locals return this; } { // Same view 'myView' with locals return this; }
Troubles
If you have any kind of trouble with it, just let me now by raising an issue on the GitHub issue tracker here:
https://github.com/arondn2/basic-controller/issues
Also, you can report the orthographic errors in the READMEs files or comments. Sorry for that, English is not my main language.
Tests
npm test
or npm run cover