Questions about identifying the components in MVC

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật