<commit_before>/*
 * middleware: Loads custom and npm middleware.
 */

var path = require('path'),
    fs = require('fs'),
    console = require('./utils');

module.exports = function(app, express){
  var config = iora.config;

  // Check and set defaults
  if (typeof config.base !== 'object') config.base = {};

  var base = config.base.middleware;
  if (typeof base !== 'string') {
    base = path.join(iora.dir, 'middleware');
  } else {
    base = path.resolve(iora.dir, base);
  }

  var middleware = config.middleware;
  if (!Array.isArray(middleware)) middleware = null;

  // Loader:
  var load = function(path){
    try {
      return require.apply(require, arguments);
    } catch (e) {
      iora.warn('Failed to load ' + path);
      return function(){};
    }
  };

  // Load npm middleware
  if (middleware) {
    middleware.forEach(function(name){
      load(name.replace(/^@/, 'iora-'))(app, config, iora, express);
    });
  }

  // Load base folder middleware
  if (base) {
    fs.readdir(base, function(err, files){
      if (!err) {
        files.forEach(function(file){
          load(path.resolve(path.join(base, file)))(app, config, iora, express);
        });
      }
    });
  }
};<commit_msg>Fixed loading of npm modules.<commit_after>/*
 * middleware: Loads custom and npm middleware.
 */

var path = require('path'),
    fs = require('fs'),
    console = require('./utils');

module.exports = function(app, express){
  var config = iora.config;

  // Check and set defaults
  if (typeof config.base !== 'object') config.base = {};

  var base = config.base.middleware;
  if (typeof base !== 'string') {
    base = path.join(iora.dir, 'middleware');
  } else {
    base = path.resolve(iora.dir, base);
  }

  var middleware = config.middleware;
  if (!Array.isArray(middleware)) middleware = null;

  // Loader:
  var load = function(path){
    try {
      return require(path);
    } catch (e) {
      iora.warn('Failed to load ' + path);
      return function(){};
    }
  };

  // Load npm middleware
  if (middleware) {
    middleware.forEach(function(name){
      load(path.join(iora.dir, 'node_modules', name))(app, config, iora, express);
    });
  }

  // Load base folder middleware
  if (base) {
    fs.readdir(base, function(err, files){
      if (!err) {
        files.forEach(function(file){
          load(path.resolve(path.join(base, file)))(app, config, iora, express);
        });
      }
    });
  }
};
{'fraction_non_alphanumeric': 0.16748366013071894, 'fraction_numerical': 0.0, 'mean_word_length': 2.773497688751926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5349818', 'n_tokens_mistral': 810, 'n_tokens_neox': 794, 'n_words': 192}