02-middleware.js
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// # 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' ];