-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
117 lines (98 loc) · 2.65 KB
/
server.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
/* eslint-disable consistent-return, max-len */
const hof = require('hof');
let settings = require('./hof.settings');
const config = require('./config.js');
const busboy = require('busboy');
const bytes = require('bytes');
const bl = require('bl');
const mockAPIs = require('./mock-apis');
const bodyParser = require('busboy-body-parser');
const _ = require('lodash');
settings = Object.assign({}, settings, {
routes: settings.routes.map(require),
behaviours: settings.behaviours.map(require)
});
const app = hof(settings);
if (config.useMocks) {
app.use(mockAPIs);
}
app.use((req, res, next) => {
// Set HTML Language
res.locals.htmlLang = 'en';
next();
});
if (config.env !== 'test') {
app.use(bodyParser({limit: config.upload.maxFileSize}));
}
if (config.env === 'development' || config.env === 'test') {
app.use('/test/bootstrap-session', (req, res) => {
const appName = req.body.appName;
if (!_.get(req, 'session[`hof-wizard-${appName}`]')) {
if (!req.session) {
throw new Error('Redis is not running!');
}
req.session[`hof-wizard-${appName}`] = {};
}
Object.keys(req.body.sessionProperties || {}).forEach(key => {
req.session[`hof-wizard-${appName}`][key] = req.body.sessionProperties[key];
});
res.send('Session populate complete');
});
}
app.use((req, res, next) => {
if (req.is('multipart/form-data')) {
let bb;
try {
bb = busboy({
headers: req.headers,
limits: {
fileSize: bytes('10mb')
}
});
} catch (err) {
return next(err);
}
bb.on('field', function (key, value) {
req.body[key] = value;
});
bb.on('file', function (key, file, fileInfo) {
file.pipe(bl(function (err, d) {
if (err || !(d.length || fileInfo.filename)) {
return;
}
const fileData = {
data: file.truncated ? null : d,
name: fileInfo.filename || null,
encoding: fileInfo.encoding,
mimetype: fileInfo.mimeType,
truncated: file.truncated,
size: file.truncated ? null : Buffer.byteLength(d, 'binary')
};
if (settings.multi) {
req.files[key] = req.files[key] || [];
req.files[key].push(fileData);
} else {
req.files[key] = fileData;
}
}));
});
let error;
bb.on('error', function (err) {
error = err;
next(err);
});
bb.on('finish', function () {
if (error) {
return;
}
next();
});
req.files = req.files || {};
req.body = req.body || {};
req.pipe(bb);
} else {
next();
}
});
module.exports = app;