-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (72 loc) · 2.41 KB
/
index.js
File metadata and controls
84 lines (72 loc) · 2.41 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
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: microgateway
// US Government Users Restricted Rights - Use, duplication or disclosure
// restricted by GSA ADP Schedule Contract with IBM Corp.
/*
* Populate the final response with the context.message
*/
'use strict';
var qs = require('qs');
var _ = require('lodash');
var logger = require('apiconnect-cli-logger/logger.js')
.child({ loc: 'microgateway:postflow' });
module.exports = function createPostFlowMiddleware(options) {
return function postflow(req, res, next) {
logger.debug('In the postflow, prepare the final response');
req.ctx.notify('post-flow', function(errors) {
try {
var msg = req.ctx.get('message');
if (msg.status) {
res.statusCode = msg.status.code ? msg.status.code : 200;
res.statusMessage = msg.status.reason;
} else {
res.statusCode = 200;
}
var contentType;
var transferEncoding;
if (msg.headers) {
res.set(msg.headers);
for (var hn in msg.headers) {
var target = hn.toLowerCase();
if (!contentType && target === 'content-type') {
contentType = msg.headers[hn];
}
if (!transferEncoding && target === 'transfer-encoding') {
transferEncoding = msg.headers[hn];
}
//early exit
if (contentType && transferEncoding) {
break;
}
}
}
var body = msg.body;
if (body && !_.isString(body) && !_.isBuffer(body)) {
if (contentType === 'application/x-www-form-urlencoded') {
body = qs.stringify(body);
} else {
body = JSON.stringify(body);
}
}
// Check if chunked mode is set
if (transferEncoding !== 'chunked') {
if (body) {
// need to get buffer length, not just length (DBCS consideration)
res.set('Content-Length', Buffer.byteLength(body, 'utf8'));
} else {
res.set('Content-Length', 0);
}
}
// Update X-Powered-By
res.setHeader('X-Powered-By', 'IBM API Connect MicroGateway');
if (body) {
res.write(body);
}
res.end();
} catch (error) {
logger.debug('Cannot read context.message in the postflow: ' + error);
next(error);
}
});
};
};