star-men-cli v1.0.1
star-men-cli
The command line interface for generating star men app.
Mongoose-Express-Nodejs (MEN)
To Use this cli to generate a star men app mongodb must be installed.
Options
| Command | Description |
|---|---|
-V, --version | output the version number |
-n, --new | generate new star-men-app |
-r --route <name> | generate new route |
-c --controller <controllerName> [routeName] | generate new controller (specify route name if it is not a global controller) |
-h, --help | output usage information |
Product Structure with template engine (ejs)
├── node_modules
├── index.js
├── package.json
├── package-lock.json
└── src
├── global-controllers
│ └── logger.controller.js
├── model-provider
│ └── models
│ └── Test.model.js
├── routes
│ ├── home
│ │ ├── controllers
│ │ │ └── index.controller.js
│ │ └── home.route.js
│ └── test
│ ├── controllers
│ │ ├── populate-object.controller.js
│ │ └── serve-results.controller.js
│ └── test.route.js
└── views
└── index.ejsProduct Structure with template engine (ejs) without mongoose injection
├── node_modules
├── index.js
├── package.json
├── package-lock.json
└── src
├── global-controllers
│ └── logger.controller.js
├── routes
│ ├── home
│ │ ├── controllers
│ │ │ └── index.controller.js
│ │ └── home.route.js
│ └── test
│ ├── controllers
│ │ ├── populate-object.controller.js
│ │ └── serve-results.controller.js
│ └── test.route.js
└── views
└── index.ejsProject structure without template engine
├── node_modules
├── index.js
├── package.json
├── package-lock.json
└── src
├── global-controllers
│ └── logger.controller.js
├── model-provider
│ └── models
│ └── Test.model.js
└── routes
└── test
├── controllers
│ ├── populate-object.controller.js
│ └── serve-results.controller.js
└── test.route.jsProject structure without template engine and monogoose injection
├── node_modules
├── index.js
├── package.json
├── package-lock.json
└── src
├── global-controllers
│ └── logger.controller.js
└── routes
└── test
├── controllers
│ ├── populate-object.controller.js
│ └── serve-results.controller.js
└── test.route.jsProject Structure Walkthrough
node_modulesThis is where all the node_modules are installed.index.jsThe starting point of the star-men-app.package.jsonUsed to manage the dependencies of modules and version of the star-men-app.package-lock.jsonUsed with package.json.srcThe source files of the star-men-app lives here.global-controllersThis is the directory where all the global controllers live. When the app is generated an examplelogger.controller.jscontroller is generatedmodel-providerThis is the directory where you manage all your application models. You can create helpers for models and the mongoose models as well.modelsThis is the directory where you create all your mongoose models. (If you use mongoose model injection it will look into this directory to find models to resolve)
routesThis is where all your routes for handling requests will live. When the cli is used, by default test route is created. We will use the test route in this example.testThe main directory for the test routecontrollersThe controllers for the test route live in this directorytest.route.jsThis is the route that lists all the controller dependencies of any request that hits test
Routes
A sample route is defined below. Route names must end with .route.js
You can use the cli to generate a route
star-men-cli -r testExample
// src/test/routes/test.route.js
const testRoute = {
routePrefix: '/api',
routes: [
{
endPoint: '/test',
method: 'GET',
controllers: [
'logger',
'populateObject',
'serveResults'
],
description: 'Tests App'
}
]
}
module.exports = testRoute;Usage
The route file is what you use to define all the endpoints of a particular route and the controllers to handle them.
Properties
routePrefix
This is used to specify the root common to all the routes of a particular route. This helps in avoiding repetition.
eg. /api will mean apply /api before all the endpoints
routes
This an array that contains all the endpoints and the controller names for a particular endpoint. They are all specified in an array of object. The format is explained in the table below.
| Property | Type | Required | Default Value | Description |
|---|---|---|---|---|
endPoint | string | true | undefined | The endpoint to handle. Note that when routePrefix is specified it will concat with the specified endPoint. |
method | string | false | GET | The method of the endpoint. You can specify any method that express supports. It is case insensitive but uppercase is recommended. |
controllers | Array<String> | true | undefined | The controllers to handle the request. Note that the request will be handled from the first controller to the last. Controllers will only be valid if it is defined in the route or in the global controller. |
description | any | false | undefined | The description of the request to be handled. |
Controllers
The controller is what handles the http request. The controller name must end with .controller.js
When mongoose model injection is used the controller must be a function that returns another function. You might wonder why we have to specify two functions, one function that returns another but, the main function can receive the injected models from mongoose when you specify {controllerName}.injectMongooseModel so that the other function can have access to that. If you disabled inject mongoose model during the configuration then you just need a valid function that represents an express middleware
Remember you can use the cli to generate a new controller.
star-men-cli -c populate-object testExample
// src/test/controllers/populate-object.controller.js
function populateObject(Test) {
return async function(req, res, next) {
const tests = await Test.find().lean();
req.testObject = {
hasTests: tests
};
return next();
};
}
populateObject.injectMongooseModel = ['Test'];
module.exports = populateObject;Example without mongoose injections
// src/test/controllers/populate-object.controller.js
function populateObject(req, res, next) {
req.testObject = {
hasTests: true
};
return next();
}
module.exports = populateObject;star-men-cli -c serve-results test// src/test/controllers/serve-results.controller.js
function serveResults() {
return async function(req, res, next) {
return res.json(req.testObject);
};
}
module.exports = serveResults;From the examples above since the first example is not responding with res the next function has to be called otherwise it will not move to the next controller. This is just how express works :)