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') {

    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)
  • Thanks for the detail on the question, but the scope borders on too-broad. Consider trying to focus on individual questions, or breaking it into many questions. You should try to grok the MVC pattern, not just the framework. Be careful to not let your controller get too big, and to keep your code SOLID. If your app works and is providing value to your users, something's right, and as you build, you'll find more specific questions to ask.
    – Chris Bye
    Commented Sep 13, 2012 at 19:34
  • @ChrisBye Which would you consider best: Editing this question or asking new ones? (also, I've already checked the wikipedia link and I wasn't using any framework specifically made for MVC)
    – luiscubal
    Commented Sep 13, 2012 at 19:46
  • 1
    I'd edit this question down to its core, and ask a new question for points that are related, but could stand on their own as a useful question (that's a good metric to use for any question on SE). Your main question is probably asking for clarity on the role of the Controller, or ways to help better adhere to good practice in the Controller and pattern at large.
    – Chris Bye
    Commented Sep 13, 2012 at 19:54
  • Examples of frameworks/tools for MVC pattern in various languages include Asp.NET MVC, FubuMVC (both .NET), and todoMVC is a collection of a number of javascript MVC projects.
    – Chris Bye
    Commented Sep 13, 2012 at 19:57
  • Edited the question. Some of the original sub-points have been removed, but hopefully it should be less broad now.
    – luiscubal
    Commented Sep 13, 2012 at 20:18

2 Answers 2


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.


  • So if I understand correctly: My session.js file can't be a view since cookies (part of HTTP headers) aren't usually directly visible by a browser user and instead it's the model - since it implements business logic of "login", "logout" and "see if it's logged in". The controller translates calls to "localhost/login" to calls to the login model function. Is this correct? And additionally, my server.js file includes the controllers, but also has many "extras" that should not belong to controllers. Is this correct?
    – luiscubal
    Commented Sep 15, 2012 at 12:55
  • But still, the definition of "seen" is weird in this case. This is a server app. The server technically does not know whether the user is using a web browser. An user could technically use telnet and then it would make sense for the view to include the cookies stuff. Am I over-thinking this?
    – luiscubal
    Commented Sep 15, 2012 at 12:59

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.