Separation of concerns in node.js
I’ve been playing with typescript and node.js and I wanted to talk a little about how I’ve broken up my app source. It’s always good to modularize an application into smaller bits, and while node lets you do a lot, quickly, with just a little bit of code, as your application grows you really can’t put all your logic in one big app.ts.
App.ts
Instead of the normal application example you see for node projects, I wanted to make it clearer what the initialization of my application does. My app start is structured like this:
/**
* Module dependencies.
*/
import db = module("./storage/storageContainer");
import auth = module("./auth/oauthDefinitions");
import requestBase = module("./routes/requestBase");
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, log = require("./utils/log.js")
, fs = require('fs')
, passport = require('passport');
var app = express();
class AppEntry{
constructor(){
this.initDb();
this.setupRoutes();
… Read more
