architecture express node.js

Building enterprise app with Node/Express

I’m trying to understand how to structure enterprise applciation with Node/Express/Mongo (actually using MEAN stack).

After reading 2 books and some googling (including similar StackOverflow questions), I couldn’t find any good example of structuring large applications using Express. All sources I’ve read suggest to split application by following entities:

  • routes
  • controllers
  • models

But the main problem I see with this structure is that controllers are like god objects, they knows about req, res objects, responsible for validation and have business logic included in.

At other side, routes seems to me like over-engineering because all they doing is mapping endpoints(paths) to controller methods.

I have Scala/Java background, so I have habit to separate all logic in 3 tiers – controller/service/dao.

For me following statements are ideal:

  • Controllers are responsible only for interacting with WEB part, i.e. marshalling/unmarshalling, some simple validation (required, min, max, email regex etc);

  • Service layer (which actually I missed in NodeJS/Express apps) is responsible only for business logic, some business validation. Service layer doesn’t know anything about WEB part (i.e. they can be called from other place of application, not only from web context);

  • Regarding to DAO layer is all clear for me. Mongoose models are actually DAO, so it most clear thing to me here.

I think examples I’ve seen are very simple, and they shows only concepts of Node/Express, but I want to look at some real world example, with much of the business logic/validation involved in.


Another thing isn’t clear to me is absent of DTO objects. Consider this example:

const mongoose = require('mongoose');
const Article = mongoose.model('Article');
exports.create = function(req, res) {
// Create a new article object
const article = new Article(req.body);
// saving article and other code

There JSON object from req.body is passed as parameter for creating Mongo document. It smells bad for me. I would like to work with concrete classes, not with raw JSON