Node.js MVC library

Hi, I’m here to talk about building a web application with Node.js using MVC technique.

Well, for starters I’ve adventured alone in Node.js trying to make a simple website, only pages after pages, easily I’ve reached that but then I needed some organization and pre defined pages, only needed to set the data before send to the client, was here that I found Partial.js.

Partial.js framework was so organized and gave me so many ways to grow my webpage functions but it had limitations, this framework was trying to use MVC(Model-View-Controller) as a way of organizing the server side application, but was a bit confusing about all the options that can be used, in some cases I tried to do what I wanted outside it.

When I was trying to do some authentication I slam in a wall, in the available API, the way that was described to do wasn’t working, so I had to investigate a bit and then I found that Partial.js framework has unfinished work on that part and the author has left it :'( Why didn’t I looked further instead of having all this work and now this framework is blocking my way!

I had two choices, redo everything and leave Partial.js or try to understand his code and repare this part. I don’t know why but I always have this curiosity to look to the code, so I did, and I found some awesome thing: the author had changed the name of the framework! So Partial.js is like Beta of TOTAL.js.

Why total? I don’t know, but to change from partial to total was practicly direct, only references and names, DONE.
And the Auth was working.

So starters have ended, I present to you TOTAL.js: http://www.totaljs.com/

As you will see this framework is based in many libraries, has a great API and examples, is full compatible with all client side frameworks, the base of it is this.

Start by the server file, the place where the server will start, so Total.js will initiate the server process and some preparation that you need.
Example:

var framework = require('total.js');
var http = require('http');
var port = 8004;
var debug = true;
framework.run(http, debug, port);
console.log('http://{0}:{1}/'.format(framework.ip, framework.port));

You need to define controllers, a way to organize it for each new pair of names in the URL you should have a new controller file.

I’ll show you a little of my own code:

exports.install = function(framework) {
framework.route('/', view_homepage);
framework.route('/contactos',view_contacts);
framework.route('/vendas',view_sells);
framework.route('/assistencia',view_assist);
framework.route('/aluguer',view_rent);
framework.route('/pecas',view_parts);
framework.route('/quemsomos',view_info);
framework.route('#404', view_404);
function view_contacts(){
this.view('contactos');
}

function view_sells(){
this.view('vendas');
}

function view_assist(){
this.view('assistencia');
}
function view_rent(){
this.view('aluguer');
}
function view_parts(){
this.view('pecas');
}
function view_info(){
this.view('quemsomos');
}

function view_404(){
console.log('entered 404');

this.view('notfound');
}

have some html files to be passed to the client as page “et voalá!”

My page in Node.js

Adventure yourselves through the documentation, it doesn’t have everything but it’s easy to understand and predict it’s behaviour.

It’s good to be human!

Luís Mendes Jorge

I'm a Software Engineer that believes Web apps are the future. I'm here to show that is not hard to develop good code in a short time period.

Your email address will not be published. Required fields are marked *