Express.js Tutorial - Beginner's Guide to Express

Here's sort of the structure I've been using that lets me easily incorporate subapps each with their own routers. I don't have it memorized verbatim, however.

goal

To have dedicated routes and subapps so I can have my own api in my dev environment.

Main application calls this route

localhost:8000/api/v1 

A subapp for my build

localhost:8000/build

And another for testing

localhost:8000/test

This reduces the grief with having a billion terminal windows open for gulp watches, karma, mocha, protractor, whatever.

structure

client/ <- front end stuff
server/ <- back end
    subapps/ <- subapps for main application, build, and testing server
        main.js
        build.js
        test.js
        SubApp.js <- constructor that creates instances of express apps
        index.js
    app.js <- mounts subapps on your main app
index.js <- initializes your app

/server/subapps/SubApp.js

/**
* takes hash of options that dictate low-level stuff like bodyParser. 
* That stuff is server-level and your "application-layer" doesn't need to manipulate it directly, only configure it. 
* Other server-level stuff like node cluster mechanisms might go here.
*/
var express = require("express");

function Server(conf) {
    this.conf = conf;
    this.server = express();
    this.router = express.Router();

    //by default use bodyParser
    if (!conf.bodyParser)
        this.server.use(bodyParser());
};

/**
* sets up paths for static assets
* the "server" performs the actual code
* your application-layer only sends configuration
*/ 

Server.prototype.use = function(conf) {
    this.server.use(conf.route, express.static(conf.path));
};

//lets you mount subapps
Server.prototype.mount = function(subapp) {
    this.server.use(subapp);
};

module.exports = Server;

/server/subapps/main.js

var Server = require("./Server");
var app = new Server();
//the app is only sending configuration details, the server implements it
app.use({route: "/images", path: "./../public/images"});
module.exports = app;

/server/subapps/index.js

module.exports = {
    main: require("./main"),
    build: require("./build"),
    test: require("./test"),
    //... etc.
};

/server/app.js

/**
* this is where the heavy application-layer logic goes
* mounting routes, controllers, models, database stuff, etc.
*/

var subapps = require("./subapps/");

var Application = {
    config: require("./config.json"), //if you have app-wide config you'd like to share throughout your app

    components: {
        controllers: {},
        routes: {},
        models: {},
        collections: {}
    },

    /**
    * register your components onto your app
    * you are basically defining your internal api here
    */

    register: function(component, name, body) {
        this.components[component][name] = body;
        return body;
    },

    init: function(port) {
        var self = this;
        var app = subapps.main; 
        var port = port || self.config.port || process.env.port;
        app.mount(subapps.build);
        app.mount(subapps.test);
        app.server.listen(port, function() {
            console.log("app started at port " + port);
        });
    }

};

module.exports = Application;

index.js

var Application = require("./server/app");
Application.init();

That's sort of how you can nicely separate server-layer with application-layer code. The general idea is that your app doesn't have to directly set up stuff like bodyParser but can dictate what is set up by communicating via the internal api you've written through the Server constructor.

/r/webdev Thread Parent Link - youtu.be