Model View Controller Pattern - Brainstorm August 23, 2012
I wanted to implement an MVC solution in my Node.js webserver. Let me describe my webserver first...
I don't typically know the names of everything I create, even if I copy the concept from another technology. But here's what it has:
I don't typically know the names of everything I create, even if I copy the concept from another technology. But here's what it has:
Server side tags (foreach, if/else, include, combine, layout, placeholder)
Page content rendering inline through ${ style } syntax. For instance ${page.title}
Servlet style "handle get" and "handle post" interface.
URL Rewriting (e.g. /item/12345/name-of-item will load (but not redirect to) /item?itemID=12345&itemKey=name-of-item
Content caching via the "If-Modified-Since" and "Date" headers
JS and CSS combining
GZIP compression on anything
Multipart and urlencoded form handling
Sessions, to store current user, etc (very extremely low very low overhead :)
Built in mobile site redirect (detect mobile and redirect to m.HOSTNAME.com)
Content sharing between full site and mobile. So it will use the same content unless otherwise specified. Keeps content/functionality duplication to a minimum
Application scope. Want to keep track of all the users currently online? Simple. Cache a list from the database that will never be updated? That's a good spot for that.
HTML templates (layouts). So you can code your overall structure once, and have pages put different pieces in "placeholders" in the layout.
.NET Style "code behind"... They aren't really, like I said in the 2nd point, they are more like Java servlets. But the front end would be /index.html and it would automatically load up /index.js and call "load" on it, unless it's a POST, then it'll call "handlePost". I like taking the best bits from technologies with which I am familiar.
This all works amazingly well, especially considering I wrote every piece except for the stuff node.js provided, and the multipart form parser. So I wanted to do an MVC as I've said. One other aspect of my webserver is content handlers. For binary files, there's a binary handler, with mime types. "JSN" is my "Jason's Server in Node" files, which are HTML with those JS "code-behind/servlet" files. I could add an "MVC" handler with it's own extension, or a setting "useMVC" that gets checked when the server loads up content. If this setting is set, load up the MVC handler instead.
Model: This part is already taken care of, as mongoDB provides an excellent data store.
View These are already implemented in JSN/HTML files.
Controller: This is the part that's tricky. I'm thinking something like this:
function Controller(modelType){
}
HIGH LEVEL OVERVIEW (I'm getting tired).... So a resource at /pianos/12345/view will say "Load up the pianos controller, give me the one with ID 12345, and show it to me." Typically MVC's main distinct characteristic is the URLs. /pianos/jason/search will search pianos with "jason". The actions will be defined as methods on the Controller, each customizeable of course. The controller will point the server to the correct view, passing the model along. For updates, it'll be /pianos/12345/update. Also, typically, MVC uses REST style requests, which means to delete a piano, you would use the DELETE HTTP method, on the URL /pianos/12345/
I am too tired for this right now, hopefully I can make sense of this post in the morning.