02-middleware.js 1.7 KB

// # middleware
 
 
var serveFavicon = require('serve-favicon');
var path = require('path');
var serveStatic = require('serve-static');
var winstonRequestLogger = require('winston-request-logger');
var methodOverride = require('method-override');
var bodyParser = require('body-parser');
var responseTime = require('response-time');
// var busboy = require('connect-busboy');


exports = module.exports = function(IoC, logger, settings, policies) {

  var app = this;

  // ignore GET /favicon.ico
  // app.use(serveFavicon(path.join(settings.publicDir, 'favicon.ico')));

  if (settings.server.env === 'development') {
  
  }

  // static server (always keep this first)
  // <http://goo.gl/j2BEl5>
  app.use(serveStatic(settings.publicDir, settings.staticServer));

  // adds X-Response-Time header
  app.use(responseTime({
    digits: 5
  }));

  // prepare req.log for error handler
  app.use(function(req, res, next) {
    req.log = {
      response_time: new Date().getTime(),
      path: req.path,
      query: req.query,
      body: req.body,
      params: req.params
    };
    next();
  });

  // winston request logger before everything else
  // but only if it was enabled in settings
  if (settings.logger.requests) {
    app.use(winstonRequestLogger.create(logger));
  }

  // parse request bodies
  // support _method (PUT in forms etc)
  app.use(
    bodyParser.json({limit: '10mb'}),
    bodyParser.urlencoded({
      limit: '10mb',
      extended: true
    }),
    methodOverride('_method')
  );
  //support "application/x-www-formurlencoded" or starts with "multipart/*"
  // app.use(busboy({  
  //     limits: {
  //       fileSize: 10 * 1024 * 1024
  //     }
  // })) 

};

exports['@require'] = [ '$container', 'igloo/logger', 'igloo/settings', 'policies' ];