$ npm install 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.
sudo npm install -g injection
injection yourApp --jade
Choose your favorite templating language: --jade, --ejs, --swig, --mustache, etc...
If no templating is defined, the default templating will be handlebars.
Edit config/development.json
or config/production.json
.
Set your environment variable NODE_ENV
accordingly.
Edit the file app/routes
. For example:
GET/user/:name -> controller/user.js
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');
}
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);
}
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>
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.
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"
},
...
}
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) {
...
}
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);
});
}
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'};
node app
To run your app as a daemon:
node app start
node app stop
node app restart
node app status
All the methods from Express can be called, ex: req.param
, res.json
.
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.
© 2010 - cnpmjs.org x YWFE | Home | YWFE