injection
MVC framework with angularJS-like dependencies injections between models, views and controllers.
Last updated 12 years ago by jie .
Repository · Original npm · Tarball · package.json
$ npm install injection 
SYNC missed versions from official npm registry.

injection

MVC framework with angularJS-like dependencies injections between models, views and controllers.
With all the functionalities of Express since it's built on top of Express.

Installation

sudo npm install -g injection

Create an app

injection yourApp --jade

Choose your favorite templating language: --jade, --ejs, --swig, --mustache, etc...
If no templating is defined, the default templating will be handlebars.

Configure your app

Edit config/development.json or config/production.json.
Set your environment variable NODE_ENV accordingly.

Define routes

Edit the file app/routes. For example:

GET/user/:name -> controller/user.js

Define controllers

Create files in the app/controller directory, ex: app/controller/user.js.
A controller is a JS file (a module) with a controller function.

//app/controller/user.js
exports.controller = function(res) {
  res.send('Hello World');
}

Define models

Create files in the app/model directory, ex: app/model/person.js.
A model is a JS file (a module) with a model function.

//app/model/person.js
exports.model = function(name, Return) {
  //Find the person with the good name from your DB
  Return(personFetchedFromDB);
}

Define views

Create files in the app/view directory, ex: app/view/user.html.
A view is just a template file from your favorite templating language: handlebars, jade, etc...
Define your favorite templating when you create the app, ex: injection yourApp --jade.

//app/view/user.html
<h1>Hello {{name}}</h1>

Define middlewares

Create files in the app/middleware directory, ex: app/middleware/yourMiddleware.js.
For example, a useless middleware that signs every request with your name:

//app/middlewares/yourMiddleware.js
exports.middleware = function(options) {
  var name = options.name;
  return function(req, res, next) {
    req.name = name;
    next();
  }
}

//config/development.json
{
  "middleware": {
    ...,
    "yourMiddleware": {
      "name": "yourName"
    },
    ...
  }
}

This is how you set a global middleware. If you want to set a middleware only for some controllers, see the section Inject middlewares.

Define initializers

Create files in the app/init directory, ex: app/init/redis.js.
For example, init your redis connection:

//app/init/redis.js
exports.init = function(options, Return) {
  var app = this;
  var host = options.host;
  var port = options.port;
  ...
  app.modelArg.redis = redisConnection;
  Return(); //when the connection is set. 
}

//config/development.json
{
  ...,
  "redis": {
    "port": 5000,
    "host": "localhost"
  },
  ...
}

Inject models and view in controllers

To inject the model in the controller, just pass the model's name as a param to your controller.
For example, if your controller user.js needs to call the model person.js:

//app/controller/user.js
exports.controller = function(res, user) {
  res.send('Hello ' + user.name);
}

//app/model/person.js
exports.model = function(Return) {
  Return({name: 'John Doe'});
}

To inject the view user.html in the controller user.js, just pass a parameter view to the controller.

//app/controller/user.js
exports.controller = function(res, user, view) {
  res.render(view, user);
}

//app/view/user.html
<h1>Hello {{name}}</h1>

Any controller can accept the specials params req, res, next and db which are respectively the request, response, nextCallback and MongoDB connection (defined in the config file).

exports.controller = function(req, res, db, next) {
  ...
}

Inject params in models

To inject any params from req.params, req.body, req.query into the model, just pass a param to the model:

//app/model/person.js
exports.model = function(name, city, Return) {
  //fetch your DB
  Return(personFetchedFromDB);
}

//app/controller/user.js
exports.controller = function(res, user) {
  res.send('Hello ' + user.name);
}

//app/route
GET/user/:name/:city -> controller/user.js

In the example above, the params name and city are injected in the model, the controller calls the model with the param user and there is a route routing to that controller.

A model must have the param Return and call it. Any model can also accept the special param db which is the MongoDB connection (defined in the config file):

//app/model/person.js
exports.model = function(name, db, Return) {
  db.person.findOne({name: name}, function(err, person) {
    Return(person);
  });      
}

Inject middlewares in controllers

Define a middleware in the app/middleware directory, ex: app/middleware/yourMiddleware.js. Call it in your controller this way:

//app/controller/user.js
exports.controller = function(req, res) {
  res.send('Hello ' + req.name);
}
exports.yourMiddleware = {name: 'yourName'};

Run your app

node app

To run your app as a daemon:

node app start
node app stop
node app restart
node app status

Compatibility with Express

All the methods from Express can be called, ex: req.param, res.json.

MIT License

Copyright (c) 2013 Jie Meng-Gerard contact@jie.fr

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 1.8.4                                ...           beta (5 years ago)
  • 1.8.6                                ...           latest (5 years ago)
  • 1.7.6                                ...           next (5 years ago)

73 Versions

  • 1.8.6                                ...           5 years ago
  • 1.8.5                                ...           5 years ago
  • 1.8.4                                ...           5 years ago
  • 1.8.3                                ...           5 years ago
  • 1.8.2                                ...           5 years ago
  • 1.8.1                                ...           5 years ago
  • 1.8.0                                ...           5 years ago
  • 1.7.7                                ...           5 years ago
  • 1.7.6                                ...           5 years ago
  • 1.7.5-beta.1                                ...           5 years ago
  • 1.7.5-beta.0                                ...           5 years ago
  • 1.7.5                                ...           5 years ago
  • 1.7.4                                ...           5 years ago
  • 1.7.3                                ...           5 years ago
  • 1.7.2                                ...           5 years ago
  • 1.7.1                                ...           5 years ago
  • 1.7.0                                ...           5 years ago
  • 1.6.1                                ...           5 years ago
  • 1.6.0                                ...           5 years ago
  • 1.5.2                                ...           5 years ago
  • 1.5.1                                ...           5 years ago
  • 1.5.0                                ...           5 years ago
  • 1.5.0-beta.1                                ...           5 years ago
  • 1.4.2                                ...           6 years ago
  • 1.4.1                                ...           6 years ago
  • 1.4.0                                ...           6 years ago
  • 1.3.2                                ...           6 years ago
  • 1.3.1                                ...           6 years ago
  • 1.3.0                                ...           6 years ago
  • 1.2.0                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 0.7.0                                ...           6 years ago
  • 0.6.5                                ...           6 years ago
  • 0.6.4                                ...           6 years ago
  • 0.6.3                                ...           6 years ago
  • 0.6.2                                ...           6 years ago
  • 0.6.0                                ...           6 years ago
  • 0.5.0                                ...           6 years ago
  • 0.4.6                                ...           6 years ago
  • 0.4.5                                ...           6 years ago
  • 0.4.5-alpha.10                                ...           6 years ago
  • 0.4.5-alpha.8                                ...           6 years ago
  • 0.4.1                                ...           6 years ago
  • 0.3.8                                ...           6 years ago
  • 0.3.2                                ...           6 years ago
  • 0.3.1                                ...           6 years ago
  • 0.3.0                                ...           6 years ago
  • 0.2.10                                ...           6 years ago
  • 0.2.7                                ...           6 years ago
  • 0.2.4                                ...           6 years ago
  • 0.2.3                                ...           6 years ago
  • 0.2.2                                ...           6 years ago
  • 0.2.1                                ...           6 years ago
  • 0.2.0                                ...           6 years ago
  • 0.1.6                                ...           6 years ago
  • 0.2.0-alpha.1663751b                                ...           6 years ago
  • 0.1.5                                ...           6 years ago
  • 0.1.4                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.7                                ...           11 years ago
  • 0.0.6                                ...           11 years ago
  • 0.0.5                                ...           11 years ago
  • 0.0.4                                ...           11 years ago
  • 0.0.3                                ...           11 years ago
  • 0.0.2                                ...           11 years ago
  • 0.0.1                                ...           12 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE