In our previous blog I explained that how to create node.js web application using locomotive framework. Now I am going to  explain the directory structure and it’s work. Below is the directory structure of the node.js web application using locomotive.

directory _structure

I will explain functionality of all files and directories but I would like to start with entry point of the application . Here the entry point is routes.js.

  • routes.js
  • You can see the code in routes.js like :

    module.exports = function routes() {

    Basically We defines all url mapping in routes.js. Here this.root(‘pages#main) is provided default it mean when we access the application from browser “http://localhost:3000” we are trying to access the root page of the application. ‘pages#main’ means that server will look into view/pages/main.html.ejs.

  • Config
  • In Config you will see the 2 directories named *environments and *initializers. In environments Directory you will find the the three files named all.js, development.js and production.js. We define the all configuration in these files. All configuration related development will go in development.js, Production related configuration will go to production.js and the common configuration which will be used in both environment will go in all.js.

    Regarding initializers you can see lots of file. Basically all file in initializers folder will be loaded at the runtime. When we use some external plugin then the configuration related that plugin goes in this file. You can create new one for other plugin and it will be executed at server startup.

  • Controllers
  • Here we define the controllers. In current application you can see a controller named pagesController.js. In all controller name “Controller” will be the suffix. In Controller you can see the code like:

    var locomotive = require('locomotive')
    , Controller = locomotive.Controller;
    var pagesController = new Controller();
    pagesController.main = function() {
    this.title = 'Locomotive';
    module.exports = pagesController;

    In above code locomotive controller is initialized and at line 6 action is defined with name main.
    At line 7 we are setting the value to controller scope and it will be available to view.
    At line 8 we have rendered the view using the code “this.render()”. As locomotive follow the convention so it will render “view/pages/main.html.ejs” file. Using convention Controller name will be the folder name inside view directory and file name will be the action name. Here controller is pagesController and action name is main then it will search for “view/pages/main.html.ejs” and all the variable set in controller will be available to that view.

  • Views
  • Views will be available via Controller.

That’s all about the Directory Structure and functionality of locomotive.

In my next blog I will show you how to connect our application to mongodb using plugin.