-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (51 loc) · 2.01 KB
/
index.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
const fs = require('fs');
const http = require('http');
const https = require('https');
const chardet = require('chardet');
const options = {};
let serverType = http;
let port = 80;
const tlsKey = 'server.key';
const tlsCert = 'server.cert';
if (fs.existsSync(tlsKey) && fs.existsSync(tlsCert)) {
options.key = fs.readFileSync(tlsKey);
options.cert = fs.readFileSync(tlsCert);
serverType = https;
port = 443
}
const server = serverType.createServer(options, function (request, response) {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
const timestamp= Date.now();
const headers = request && request.headers;
// console.log('FULL REQUEST: ', request);
let charSet;
body = Buffer.concat(body).toString();
try {
charSet = chardet.detect(Buffer.from(body));
} catch (e) {
console.log('error attempting to detect character set: ', e);
}
const urlMethod = `Request Method: ${request.method} || URL: ${request.url} || Body character Set: ${charSet}`;
console.log(`******************** REQUEST BEGIN **********************`);
console.log(urlMethod);
console.log(`*********************************************************`);
console.log('********************* BODY BEGIN ************************');
console.log(body);
console.log('********************* BODY END **************************');
console.log(`*********************************************************`);
console.log('********************* HEADERS BEGIN *********************');
console.log(headers);
console.log('********************* HEADERS END ***********************');
console.log('***************** DETECTED CHARACTER SET ****************');
console.log(charSet);
console.log('******************* REQUEST END *************************');
fs.writeFileSync(`${timestamp}-request.txt`,urlMethod+'\n\n'+'Headers:\n'+JSON.stringify(headers));
fs.writeFileSync(`${timestamp}-body.txt`,body);
response.end();
});
})
server.listen(port);
console.log('requestLogger listening on port '+port)