Monday, July 9, 2012

NodeJs - Basic Server Implementation With MVC Architecture

Here, I come back with Nodejs and MVC concept. Yes, I want to use Nodejs modules more easy and flexible way. Before get into deep, just go through the following diagram.


Unlike PHP+Apache, In NodeJs, the code is live until the application terminate once application is started. So any careless code will leads to memory-leaks. So before start, we have to plan about structure of project. That is, there should be clear definition about place of modules and how they are going to communicate each other. So here, I have proposed a design with support MVC which is showed in diagram. There is three kind of modules, First one is core modules like http, fs and more. Second one is contributed modules like request, underscore and more. And third type is custom modules which are developed by application developer. I have to organize these three modules in right folder/directory. Luckily, Core modules are comes with NodeJs installation setup. And Contributed modules are placed in folder named with "node_modules" by NPM. So I have to take care only about custom modules.

Here, I feel keep those custom modules or files in separate folder without joining them as a part of contributed module. I called this separate folder as "Application" which is showed in diagram. Since I have control over my code, I divide them three part to support MVC and organize the code in files based on their responsibilities.

And more, as showed in diagram, I would like to keep the communication of those modules in application folder more strong and communication of modules between "Application" folder and "node_modules" folder is in comfortable level. I mean, On incoming request the modules in "Application" folder talks strongly within themselves. In another word, during the application start-up, the modules within "node_modules" folder interact each other and generate a base. The modules in "Application" folder are interact often, that means on every request. Since "Express" module has great control on handling routing, session and request body parsing, I am using "Express" to gain controller advantage. Overall, The system handles the request with respective to MVC.

Github: https://github.com/hilarudeens/NodeJs-BasicServer-MVC
Note: You can fork it at anytime. Help to improve the code.


Output: Folder view



Note: Please write your queries and suggestion as comment.


No comments:

Post a Comment