Fork me on GitHub

diet 0.9

Modules

level:
duration:
15 minutes

Table of Contents

# Intro

Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.


Conceptually, modules represent a separation of concerns, and improve maintainability by enforcing logical boundaries between components.

# Installing Modules

You can install modules by copying the module's folder into your project's node_modules folder. However you find can install node modules easier with NPM.


    # Navigate to your project
    cd /path/to/yourProject
	
    # Install a static module designed specifically for diet
    npm install diet-static
	

# Using Modules

If your modules need to interact with diet's signal, then you need to attach them to your routes as middleware functions.


A Static Server

    // Setup server
    var server = require('diet')        // Require diet
    var app = server()                  // Create Server Instance
    app.listen('http://localhost/')     // Listen on localhost port 80
    
    // Require the diet-static module and configure it
    var static = require('diet-static')({ path: app.path+'/static/' }) 
    
    // Attach the static module as a footer middleware
    app.footer(static)
    
    // This is a working static file server
    // anything you put in ../yourProject/static/
    // will be served as a file when requested
    // for example: to load an image from "../~yourProject/static/cat.png"
    // you can send a GET request to "http://localhost/cat.png"
	


Determine login state with Cookies

    // Setup server
    var server = require('diet')        // Require diet
    var app = server()                  // Create Server Instance
    app.listen('http://localhost/')     // Listen on Localhost
	
    // Require the diet-static module and configure it
    var cookie = require('diet-cookies')
	
    // Attach the cookie module as a header middleware
    app.header(cookie)
	
    app.get('/', function($){
        if($.cookies.id){
            $.end('logged in')
        } else {
            $.end('logged out')
        }
    })
	

# Writing Modules

To have a complete understanding of how modules work in node.js read the official API reference.



Add Module Example

Let's create a very simple module that creates an $.add function when it is attached to a route.


    // yourProject/node_modules/myModule/index.js
    module.exports = function($){
        $.add = function(a,b){
            return a + b
        }
        $.return()
    }
	

Now in your index file at yourProject/index.js you can create the server and load your new module.


    // yourProject/index.js
	
    // Setup server
    var server = require('diet')        // Require diet
    var app = server()                  // Create Server Instance
    app.listen('http://localhost/')     // Listen on Localhost
	
    // Require the diet-static module and configure it
    var add = require('myModule')
    
    // Register the add middleware 
    // and an anonymous function
    // to "GET /" path
    app.get('/', add, function($){
    	var result = $.add(10,15)
    	$.end(result) // -> 25
    })
	


Add Module Example with Options

What if we want to call our module with options? That can be solved in a few ways a very simple solution is to return a callback in module.exports.


    // yourProject/node_modules/myModule/index.js
    module.exports = function(options){
        function callback($){
            $.add = function(a,b){
                return options.prepend + (a + b)
            }
            $.return()
        }
        return callback
    }
	

Now in your index file at yourProject/index.js you can create the server and load your new module.


    // yourProject/index.js
	
    // Setup server
    var server = require('diet')        // Require diet
    var app = server()                  // Create Server Instance
    app.listen('http://localhost/')     // Listen on Localhost
	
    // Require the diet-static module and configure it
    var add = require('myModule')({ prepend: 'Hello World' })
	
    // Register the add middleware 
    // and an anonymous function
    // to "GET /" path
    app.get('/', add, function($){
        var result = $.add(10,15)
        $.end(result) // -> Hello World 25
    })
	

# What's next?

Congratulations, you completed all the tutorials! The next step is to build something on your own be it a website, an app, an api server or just a new module. Whatever you will build, good luck!