I’m currently developing an client-server application in node.js, Express, mustache and MySQL. However, I believe this question should be mostly language and framework agnostic.
This is the first time I’m doing a real MVC application and I’m having trouble deciding exactly what means each component.
(I’ve done web applications that could perhaps be called MVC before, but I wouldn’t confidently refer to them as such)
I have a server.js
that ties the whole application together. It does initialization of all other components (including the database connection, and what I think are the “models” and the “views”), receiving HTTP requests and deciding which “views” to use.
Does this mean that my server.js
file is the controller? Or am I mixing code that doesn’t belong there? What components should I break the server.js
file into?
Some examples of code that’s in the server.js file:
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'sqlrevenge',
database : 'blog'
});
//...
app.get("/login", function (req, res) { //Function handles a GET request for login forms
if (process.env.NODE_ENV == 'DEVELOPMENT') {
mu.clearCache();
}
session.session_from_request(connection, req, function (err, session) {
if (err) {
console.log('index.js session error', err);
session = null;
}
login_view.html(res, user_model, post_model, session, mu); //I named my view functions "html" for the case I might want to add other output types (such as a JSON API), or should I opt for completely separate views then?
});
});
I have another file that belongs named session.js
. It receives a cookies
object, reads the stored data to decide if it’s a valid user session or not. It also includes a function named login
that does change the value of cookies.
- First, I thought it would be part of the controller, since it kind of dealt with user input and supplied data to the models.
- Then, I thought that maybe it was a model since it dealt with the application data/database and the data it supplies is used by views.
- Now, I’m even wondering if it could be considered a
View
, since it outputs data (cookies are part of HTTP headers, which are output)
6
Well, the most certain way to remember the details behind MVC is to learn the MVC song.
Another simple approach is:
- If you can see it, it is part of the view.
- If it implements business logic, it is the model.
- If it converts a gesture or action in the UI to a set operation sent to the model, it is part of the controller.
I also like the description at this page.
http://c2.com/cgi/wiki?ModelViewController
2
It is definitely considered best practice to keep the views clean from control logic and lower-level concerns such as cookies and session. Typically those concerns are handled by the controller calling helper methods or services to determine the logical flow (session) or return view models (ASP.Net MVC) or instance variables (Rails) that will be passed to the view. The repeated calls to these services or session can be further reduced by the use of the concept of filters (before_filter in Rails and Action Filters in ASP.Net) that are implicitly called before the invocation of the controller.
So those types of concerns are more closely associated with the controller than the view and the use of helpers or services can keep the controller clean of repetitive code.