As everyone the 1st time I start to use express, there was many problem i have to solve as
- Database
- App structure
- Router
- Separate modules
- ...
So hope this library help someone like me.
Any ideas are appreciated.
##Features
- MVC
- This library just help you to structure your code and scale up later
- No Express or ORM hack
- Config Express and ORM by yourself (Fully control)
##Installation
npm install express-orm-mvc --save
##App 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 example folder
##How to use
Please check example folder or follow these document
###Init
require(express-orm-mvc)({
path: __dirname
}, function(err){
if(err) {
console.log(err);
return;
}
console.log('done');
});
###Models
A model file should be like this
module.exports = function (orm, db) {
};
Example:
models/post.js
module.exports = function (orm, db) {
var Post = db.define('post', {
title: { type: 'text' },
content: { type: 'text' }
});
};
Check ORM document Defining Models
###Controllers
A controller file should be like this
module.exports = {
};
Example:
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);
});
}
};
Note: you can list all of your models in req.models
###Settings
config/settings.js
A settings file should be like this
module.exports = {
mode1: {
ip: <ip>,
port: <port>,
db:
},
mode2: {
ip: <ip>,
port: <port>,
db:
}
};
Example:
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: 'express-orm-mvc-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: 'express-orm-mvc-test',
connectionLimit: 100
}
}
};
Note: You should set your NODE_ENV variable (development or production), or you can by pass by send directly the mode option when init, check here
Check ORM document Connecting to Database
###Express config
config/express.js
A express config file should be like this
module.exports = function(app) {
};
Example:
module.exports = function(app) {
app.set('title', 'testing');
app.set('views', '../views');
app.set('view engine', 'ejs');
app.use(express.favicon());
};
Check Express document api
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/orm.js
A orm config file should be like this
module.exports = function(orm, db) {
};
Example:
module.exports = function(orm, db) {
db.settings.set('test', 'testing data');
};
Check ORM document Settings
Note: Library will sync database automatically.
###Routes config
config/routes.js
A routes config file should be like this
module.exports = function(app, controllers) {
};
Example:
module.exports = function(app, controllers) {
app.get( '/' , controllers.post.home);
app.get( '/post' , controllers.post.get);
app.post( '/post' , controllers.post.create);
};
##Init with options
require(express-orm-mvc)({
mode: 'development'
path: __dirname
}, callback);
##Todo