x-mvc
today i will give a small description on a recent cooked mvc package this is developed for node.js and also this is so easy just download and use it and also it development on going for more better test you can check this in -https://www.npmjs.org/package/x-mvc
https://github.com/netai/x-mvc
(if you have any idea or suggestion please write in comment it is very great full for me)
this is a simple node.js mvc structure this is a extended version of just-mvc it is more better of just-mvc. in this mvc we have added more feture like:
- create slug
- flush message
- more comfortable structure
- Database(default mysql)
- App structure
- Router
- Separate modules
- ...
Any ideas are appreciated.
Features
- MVC
- This library just help you to structure your code
- No Express or ORM hack
- Config Express and ORM by yourself (Fully control)
Dependencies
By default:You can specify those dependencies version by option, please refer to this
Installation
npm install x-mvc --save
Database packagefor mysql no need to proccess bellow step (default database mysql)
npm install <your database package>
//example
npm install mysql
Refer to ORM document Connecting to DatabaseApp structure
/
models/ -- all of your models here
controllers/ -- all of your controllers here
views/
config/
express.js -- your express config
orm.js -- your orm config
routes.js -- router
settings.js -- app settings (ip, port, database, ...)
app.js -- root
Please check exampleHow to use
Please check example or follow these documentInit
require(x-mvc)(function(err){
if(err) {
console.log(err);
return;
}
console.log('done');
});
Models
A model file should be like thismodule.exports = function (orm, db) {
//define your orm model here
};
models/post.js
module.exports = function (orm, db) {
var Post = db.define('post', {
title: { type: 'text' },
content: { type: 'text' }
});
};
Controllers
A controller file should be like thismodule.exports = {
//define your controller here
};
controllers/post.js
module.exports = {
home: function(req, res, next){
res.send('home page');
},
get: function(req, res, next) {
req.models.post.find(function(err, data) {
res.send(data);
});
},
create: function(req, res, next) {
req.models.post.create({
title: 'title',
content: 'content'
}, function(err, result) {
res.send(result);
});
}
};
Settings
config/settings.js
A settings file should be like thismodule.exports = {
mode1: { //development
ip: <ip>,
port: <port>,
db: // orm database setting object
},
mode2: { //production
ip: <ip>,
port: <port>,
db: // orm database setting object
}
};
module.exports = {
development: {
ip: '127.0.0.1',
port: 8080,
db: {
host: '127.0.0.1',
port: 3306,
protocol: 'mysql',
user: 'root',
password: '123456789',
database: 'test',
connectionLimit: 100
}
},
production: {
ip: '127.0.0.1',
port: 8080,
db: {
host: '127.0.0.1',
port: 3306,
protocol: 'mysql',
user: 'root',
password: '123456789',
database: 'test',
connectionLimit: 100
}
}
};
Check ORM document Connecting to Database
Express config
config/express.js
A express config file should be like thismodule.exports = function(app, express) {
//any express config here
};
module.exports = function(app, express) {
app.set('view engine', 'ejs');
};
Note:
- As you see there is no
views
folder in app structure, so create and manage by yourself - Library will start a server automatically, so no need this kind of this stuff
http.createServer(app).listen(function(){});
ORM config
config/dbConfig.js
A orm config file should be like thismodule.exports = function(orm, db) {
//any orm config here
};
module.exports = function(orm, db) {
db.settings.set('test', 'testing data');
};
Note: Library will sync database automatically.
Routes config
config/routes.js
A routes config file should be like thismodule.exports = function(app, controllers) {
//routes here
};
module.exports = function(app, controllers) {
app.get( '/' , controllers.post.home);
app.get( '/post' , controllers.post.get);
app.post( '/post' , controllers.post.create);
};
Options
require(x-mvc)({
mode: 'development', //default: production
path: __dirname, //default: auto detect
express: require('express'), //specify your express version
orm: require('orm') //specify your orm version
}, callback);
require(x-mvc)({
mode: 'development',
path: '/Code/Project',
}, callback);
Return object
express
orm
server
web server instancedatabase
orm database instanceapp
express app instancesettings
the current settingsmode
the current moderequire(just-mvc)(functiom(err, mvc) {
mvc.express;
mvc.orm;
mvc.server;
mvc.database;
mvc.app;
mvc.settings;
mvc.mode;
});
Notes
For your convenience, you can get-
models
: all the orm models -
settings
: the running setings -
mode
: the running mode
from express req
function (req, res, next) {
req.models;
req.settings;
req.mode;
}
from express config file
//config/express.js
module.exports = function(app, express, mvc) {
mvc.mode
mvc.settings
};
from orm config file
//config/orm.js
module.exports = function(orm, db, mvc) {
mvc.mode
mvc.settings
};
New Feature
Flush Messagefor flush message follow bellow step
when you redirect or show view then first save this two data in session
req.session.msg="message";
req.session.css_class="css-class";
next send bellow variable to view and show
msg=req.utility.flushMsg(req)
Slugcreate a slug follow bellow step
slug=req.utility.slug(product_name)