ToyFW.js is a MVC framework for node.js that is currently in development. ToyFW uses Express to create servers and has a built-in dispatcher to route controllers. The model part is not yet implemented.
- Multidomain
- MVC
- Multi language
- Highly configurable
server.js :
require('./toyfw.js')({
domains: {
'*mysite.com': {}
},
port: 80
ip: '123.45.67.89'
});
And then start the server :
$node server.js
You can even do shorter :
require('./toyfw.js')('*mysite.com');
// or redirecting multiple domains to one site :
require('./toyfw.js')(['*mysite.com', '*mysites.com']);
As no configuration is passed, it listens on localhost:80. And by default, the local directory is where should be controller/ and view/.
./
server.js
controller/
default.js
contact.js
errors.js
lang/
en/
name.lang.js
fr/
name.lang.js
view/
index.html
contact.html
public/
img/
css/
js/
The domains section accepts several types of data.
domains: '*mysite.com'
or
domains: ['*mysite.com', '*test.com']
or
domains: {
'*mysite.com': {},
'*test.com': {}
}
or
domains: {
'*mysite.com': {
srv: function(app, express) {
app.use(app.router);
app.use(express.logger());
app.use(express.bodyParser());
}
},
'*test.com': {}
}
or
domains: {
'*mysite.com': {
srv: function(app, express, cfg) {
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({ secret: cfg.secret }));
},
cfg: {
path: '/home/path/to/mysite',
secret: 'lolcat',
cache_client: 'public, max-age=3600',
cache_controller: false,
lang: 'en' // Default language
}
}
}
or
domains: {
'*mysite.com': require('./toyfw.js')({
srv: function(app, express) {
app.use(app.router);
app.use(express.logger());
app.use(express.bodyParser());
}
})
}
or
domains: {
'*mysite.com': app // Where app is an Express server
}
A controller must be in the ./controller/ folder. If mysite.com/test is requested, then the controller test will be loaded. If it does not exist, then the default controller will be loaded.
If not redefined in the cfg section, controller/default.js is the default controller.
A controller must be defined like this :
controller/default.js :
module.exports = function () {
return 'This text is generated from the controller';
};
A controller can hide himself by returning the number 404. Exemple :
controller/secure.js :
var isAllowed = function() { ... };
module.exports = function () {
if(!isAllowed()) {
return 404;
} else {
return 'Content of the secret page';
}
};
Views are in the ./view/ folder. this.mvc.template uses the underscore.js template engine.
Load a view with template :
controller/default.js :
module.exports = function () {
return this.mvc.template('index', {message: 'Test Message!'});;
};
view/index.html :
<b>The message is :</b> <%=message%>
output :
The message is : Test Message!
Lang files are in the ./lang/ folder. Each lang is in its folder (like ./lang/en/). Usage :
controller/default.js :
module.exports = function () {
this.mvc.langFile('mainwords');
return this.mvc.template('index', {userName: 'admin'});;
};
view/index.html :
<h1><%=lang('welcome', { user: userName })%></h1>
<p><%=lang('connected', { count: Math.round(Math.random()*800) })%></p>
lang/en/mainwords.lang.js :
module.exports = {
'welcome': 'Welcome {user}!',
'connected': '{count} users are online at this moment.'
};
output :
Welcome admin!
327 users are online at this moment.