Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advice on creating a micro-framework in JavaScript

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.

like image 764
donald Avatar asked Sep 15 '10 17:09

donald


People also ask

What is JavaScript Micro framework?

What is a JavaScript framework? JavaScript frameworks are the vessels emboldening the language to do its best work with little to no configuration. JavaScript being one of the most popular languages of time past and now, it's a favorite for web development both on the front and back-end.

Which JavaScript framework is best for beginners?

Vue. js. When I posed the question of which JS framework to learn first—both internally to Skillcrush developers and externally to other JS pros—Vue. js was consistently mentioned as a solid option.


1 Answers

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:

  • A giant list of node modules. Includes templates, database, routing, and full frameworks.
  • MVC with Node.js - Which modules?
  • Root.js : A Skeletal MVC Framework for Node.js
like image 106
Corbin March Avatar answered Sep 28 '22 20:09

Corbin March