7

I'd like to know what are the steps required to create a framework on top of node.js. I believe this can be a good way to learn, that's why I'm doing this!

I've been checking other micro-frameworks and bigger frameworks but I'm not being able to understand where to start. I'd like your advice on this.

Edit: MVC Framework like Sinatra, Merb, Rails.

5
  • A micro-framework to do what? Commented Sep 15, 2010 at 17:32
  • @MooGoo: Since he's talking server-side code it's probably an MVC or MVVM framework. Like Rails or Django or Catalyst. Commented Sep 15, 2010 at 18:38
  • See also: Is there a Javascript MVC (micro-)framework? Commented Sep 15, 2010 at 18:42
  • 1
    Thanks, but that doesn't explain how to do one! :/ Commented Sep 15, 2010 at 19:34
  • Let me know if you ever finish this. ;) Commented Sep 15, 2010 at 22:26

1 Answer 1

5

For an MVC framework, the basic concepts go something like this (forgive the simplicity):

var view = 'I say, "{{first}} {{second}}".';
var model = {
    first: 'hello',
    second: function(){
        return 'world';
    }   
};

for(item in model){     
    var regex = new RegExp('{{' + item + '}}', 'gi');
    if(typeof(item) == 'function')
        view = view.replace(regex, model[item]());
    else
        view = view.replace(regex, model[item]);
}
console.log(view);

Start as simple as possible and add small enhancements:

  • Store views/templates as files. This gives you a chance to play with node.js's async file I/O.
  • Add support for more complex models - repeating items/arrays, objects containing objects
  • Add support for templates inside templates
  • Fetch your models from an external datasource. CouchDB might be fun.
  • Add proper controllers - these objects should know which models go with which views and how to stitch them together
  • Map your Http request urls to controllers and actions - /person/55 might fetch a person with id 55 from your data repository, /person/add might bring up a UI to add a person - both use a person controller with views displayed for the appropriate action.

Take a look at mustache.js for a small template engine. Note their terminology differs from mine in examples and code. What I call a view, they call a template and what I call a model, they call a view. It's a small thing but potentially confusing.

Additional resources:

Sign up to request clarification or add additional context in comments.

2 Comments

wow, very nice comment! Do you have any resources that would help me learn this better? If there isn't any JavaScript related, maybe something related to Sinatra, Rails could be helpful! Thanks Corbin.
@donald - added a big 'ole list of node modules. Have fun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.