pomegranate-express

1.1.0 • Public • Published

Pomegranate-Express

A Plugin bundle for the Pomegranate application framework. Pomegranate-Express bundles several easily configurable plugins that implement the Express framework.

Usage and Examples

Install

$ npm install --save pomegranate pomegranate-express
$ ./node_modules/.bin/pomegranate init -f . && ./node_modules/.bin/pomegranate build
$ node pom.js

Routing.

The Router plugin creates your route structure based on directory and module names. You can basically treat it exactly like an instance of Express Router because that is exactly what it is.

ex.

/* file ./routes/index.js */
 
module.exports = function(Router){
 
  //available at http://localhost:8080/
  Router.get('/', function(req, res, next){
    res.send('Hello from the index')
  })
 
  // Note: your route files must return
  // the factory instance of Router provided
  return Router
}
 

You can define named routes just as easily.

/* file ./routes/hello/index.js */
 
module.exports = function(Router){
  //available at http://localhost:8080/hello
  Router.get('/', function(req, res, next){
    res.send('Hello from a sub directory')
  })
 
  //available at http://localhost:8080/hello/world
  Router.get('/world', function(req, res, next){
    res.send('Hello from a named route')
  })
  return Router
}

Finally You can name your files if you have complex routes that you wish to segregate. These coexist with index files, so feel free to experiment with what works best for you.

/* file ./routes/hello/name.js */
 
module.exports = function(Router){
  //available at http://localhost:8080/hello/name/bob
  Router.get('/:name', function(req, res, next){
    res.send('Hello from a named file ' + req.params.name)
  })
  return Router
}
 

Middleware.

Obviously any Express application lives on its middleware stack. Pomegranate makes it easy to create, configure and load your middleware functions without a huge boilerplate file.

The Application Plugin creates a shared object that middleware functions can merge into, Later in the Pomegranate bootstrap process, the PreRouter and PostRouter plugins will use this object to mount your middleware. The BundleMiddleware plugin provides some stock functions, but you can easily override them by creating your own with the same name.

ex.

/* file ./plugins/MyMiddleware.js */
 
module.exports = {
  options: { //optional but preferred}
  metadata: {
    name: 'MyMiddleware',
    layer: 'dependencies'
    param: 'Middleware',
    type: 'merge'
  },
  plugin: {
    load: function(inject, loaded){
 
      /*
       * Create one or many middleware functions here, you can split them up into separate files if needed.
       */
 
      var myMiddlewares = {
       awesome: function(req, res, next){
          console.log('You are really awesome')
          next()
       }
     }
 
     /*
      * When you are ready, call loaded with your middlewares as the second param.
      * If something goes wrong, pass the error to the first param of loaded()
      */
 
     loaded(null, myMiddlewares)
    },
    start: function(done){
      done(null)
    }
    stop: function(done){
      done(null)
    }
  }
}

Included Plugins

Application

Provides the Express App, Express Router Factory and Middleware object.

Injector: Dynamic Adds a dynamic number of objects.
Injects: Service Express - Configurable instance of Express.
Injects: Factory Router - Provides instances of Express.Router() to define routes on.
Injects: Merge Middleware - An object spanning all of the middleware in a project.
Properties

Name Type Default Description
options Object Plugin Options
options.caseSensitiveRoutes Boolean false
options.mergeReqParams Boolean false
options.strictRouting Boolean false

BundledMiddleware

Provides Overrideable standard middleware.

Injector: Merge Merges all returned objects into the provided dep name.
Injects:: function Middleware - 404
Injects:: function Middleware - 500
Properties

Name Type Description
options Object This plugin has no options

PreMiddleware

Configures the Express application and mounts all pre-route middleware.

Injector: None Adds nothing to the injector.
Properties

Name Type Description
options Object Plugin Options
options.order Array Mount order of middleware functions.

Router

Loads and mounts provided route definition files located in options.workDir

Injector: None Adds nothing to the injector.
Properties

Name Type Default Description
options Object Plugin Options
options.workDir String ./routes Directory to load routes from.

PostMiddleware

Configures the Express application and mounts all post-route middleware, including error handlers.

Injector: None Adds nothing to the injector.
Properties

Name Type Default Description
options Object Plugin Options
options.order Array.<String> '['404','500']' Mount order of middleware functions.

Launcher

Configures the Express application and mounts all pre-route middleware.

Injector: None Adds nothing to the injector.
Properties

Name Type Default Description
options Object Plugin Options
options.port Number 8080 Port to bind server to.
options.address String localhost Address to bind server to.

Bundled Middleware

404

Bundled 404 handler middleware

500

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.0
    0
    • latest

Version History

Package Sidebar

Install

npm i pomegranate-express

Weekly Downloads

0

Version

1.1.0

License

MIT

Last publish

Collaborators

  • paperelectron