Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Node.js application directory structure

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 415
    Comment on it

    Node.js does not come with any predefined folder structure thus in order to architect your application it entirely depend on your nature of choice. Before architecting any application you should keep in mind the nature and scope of an application.

     

    As you applications grows in size it having a poor architect can lead to following issues :

     

     -> Code size
     -> Code complexity
     -> Difficulty understanding code
     -> Harder for team to work simultaneously on code
     -> Conflicts in code
     -> Difficulty adding new features
     -> Hard to debug application
     


    So in order to get yourself out of the above situation we need to have well defined architect in which we should keep note of following points, thus making a clean architecture :

     

     -> Proper understandable folder structure , it means you cant just throw files to any folder like this for example lib and keep on increasing its size. All files should be placed in a well defined manner in their respective folders if possible following some kind of universal approach. Such that nay new developer coming on the project can easily understand it and can add any new file as needed.


      -> Following consistent and meaningful naming convention for example if you are making services it should be name something like EventService other then EventModule which does not make much sense.


      -> Proper code hierarchy , code should be assembled in proper hierarchical manner such that it can easily traversed from top most to lowest access layer.


      -> Code should be easy to test such that each module can be tested separately without having any dependency on each other.

     

    As conveyed earlier architecture of an application depend on individual choice. Lets give you an idea of widely used node architecture based on a application having user login,registration and comments :

     

    project/
      controllers/
        comments.js
        index.js
        users.js
      helpers/
        dates.js
      middlewares/
        auth.js
        users.js
      models/
        comment.js
        user.js
      public/
        libs/
        css/
        img/
      views/
        comments/
          comment.jade
        users/
        index.jade
      tests/
        controllers/
        models/
          comment.js
        middlewares/
        integration/
        ui/
      .gitignore
      app.js
      package.json

     

    Below is some description about application architecture being used above:

     


        controllers/ – defines your app routes and their logic
        helpers/ – code and functionality to be shared by different parts of the project
        middlewares/ – Express middlewares which process the incoming requests before handling them down to the routes
        models/ – represents data, implements business logic and handles storage
        public/ – contains all static files like images, styles and javascript
        views/ – provides templates which are rendered and served by your routes
        tests/ – tests everything which is in the other folders
        app.js – initializes the app and glues everything together
        package.json – remembers all packages that your app depends on and their versions

     

    But the above approach do have some cons :


        -> to understand how the product pages work, you have to open up three different directories, with lots of context switching,
        -> you end up writing long paths when requiring modules: require('../../controllers/user.js')


    In order to overcome this issue we can architect our application around features / components

    project/
      user/
        user_controller.js
        user_model.js
        index.js
        index.jade
        user.spec.js

    Above approach really help developer to get all things related to particular module as a location he need not to look at different location just to look for a simple functionality.

    Although two approaches being suggested above are your individual preferences.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: