Questions about identifying the components in MVC

Posted by luiscubal on Programmers See other posts from Programmers or by luiscubal
Published on 2012-09-13T16:53:08Z Indexed on 2012/09/13 21:49 UTC
Read the original article Hit count: 303

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)

© Programmers or respective owner

Related posts about architecture

Related posts about language-agnostic