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
- Model View Controller (MVC)
- Highly configurable
- Internationalization
server.js :
require('./toyfw.js')({
domains: {
'*abcde.com': {
srv : function (app, express, cfg) {
app.use(app.router);
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({ secret: cfg.secret }));
},
cfg : {
path: '/srv/abcde.com',
host: 'abcde.com',
secret: 'lolcat',
controller: 'default', // default controlleur
cache_client: 'public, max-age=3600',
cache_controller: false,
lang: 'fr'
}
}
},
port: 80,
ip: '127.0.0.1'
});
And then start the server :
$node server.js
As no configuration is passed, it listens on 127.0.0.1:80. And by default, the local directory is where should be controller/ and view/.
./
toyfw.js
server.js
abcde.com/
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/
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.