1.0.1 • Published 4 years ago

star-men-cli v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

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

CommandDescription
-V, --versionoutput the version number
-n, --newgenerate 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, --helpoutput 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.ejs

Product 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.ejs

Project 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.js

Project 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.js

Project Structure Walkthrough

  • node_modules This is where all the node_modules are installed.
  • index.js The starting point of the star-men-app.
  • package.json Used to manage the dependencies of modules and version of the star-men-app.
  • package-lock.json Used with package.json.
  • src The source files of the star-men-app lives here.
    • global-controllers This is the directory where all the global controllers live. When the app is generated an example logger.controller.js controller is generated
    • model-provider This is the directory where you manage all your application models. You can create helpers for models and the mongoose models as well.
      • models This 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)
    • routes This 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.
      • test The main directory for the test route
        • controllers The controllers for the test route live in this directory
        • test.route.js This 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 test

Example

// 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.

PropertyTypeRequiredDefault ValueDescription
endPointstringtrueundefinedThe endpoint to handle. Note that when routePrefix is specified it will concat with the specified endPoint.
methodstringfalseGETThe method of the endpoint. You can specify any method that express supports. It is case insensitive but uppercase is recommended.
controllersArray<String>trueundefinedThe 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.
descriptionanyfalseundefinedThe 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 test

Example

// 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 :)

1.0.1

4 years ago

1.0.0

4 years ago