Controller folder inside expressJS.

Like most of the developers I love MVC architecture in web framework. In ExpressJS it does have a view folder but missing the model and the controller folder.

I’m a fairly new to NodeJs (expressjs) , coming from PHP background and with lots of love with Laravel framework. It seems in expressjs controller folder is missing and i went ahead and created one. I usually create controller folder and app folder inside directory.


And the code snippets

var pathModule = require('path');
var fs = require('fs');
exports.pathLocation = function () {
    var basePath = pathModule.resolve(__dirname,'../controllers');
    var controllers = {};
    var paths = fs.readdirSync(basePath);
      var files = fs.readdirSync(pathModule.join(basePath, path));
     controllers[path] = {};
     files = files.filter(function(file){ 
return !(file.indexOf('.')===0 ); });
     files.forEach(function(file) {
     var key = file.replace(/\.\w{2,3}$/, '');
     controllers[path][key] =
require(pathModule.join(basePath, path, file)); });
    return controllers;

Assume we a have a file called article.js inside controllers/app directory and we can access in routes such as

var control = require('../helper/controllers')
var controllers = control.pathLocation();
/* GET home page. */

this way its really organized and easy to read the codes.

thats it and thanks for reading.