-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarma.middleware.js
37 lines (33 loc) · 1.04 KB
/
karma.middleware.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
/* eslint-disable func-names, no-underscore-dangle */
const queryString = require('query-string');
module.exports = function() {
return function(request, response) {
let body = '';
request.on('data', chunk => {
body += chunk.toString(); // convert Buffer to string
});
request.on('end', () => {
const query = queryString.parse(request._parsedUrl.query);
const params = queryString.parse(body);
if ('status' in query) {
response.setHeader('Content-Type', 'application/json');
response.setHeader('X-Status-Code', query.status);
}
let responseText;
switch (request._parsedUrl.pathname) {
case '/json':
responseText = JSON.stringify({
message: query.message || params.message || 'test'
});
break;
case '/postJSON':
responseText = JSON.stringify({
message: body ? JSON.parse(body).message : 'no-body'
});
break;
default:
}
response.end(responseText);
});
};
};