-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-log.js
32 lines (30 loc) · 1.11 KB
/
http-log.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
#! /usr/bin/env node
var port = process.argv[2] || 7777
var statusCode = process.argv[3] || 200
var answer = process.argv[4]
require('http').createServer(function(request, response) {
console.log('--------------------------------------------')
console.log('Request started:', new Date)
console.log('Url:', request.url)
console.log('Method:', request.method)
console.log('Headers:', request.headers)
console.log('---------------- body start ----------------')
request.on('error', function(err) {
console.error('\nError on request', err)
}).on('data', function(chunk) {
process.stdout.write(chunk.toString())
}).on('end', function() {
console.log('\n---------------- body end ----------------\n')
response.on('error', function(err) {
console.error('\nError on response', err)
})
response.statusCode = statusCode
if (answer) {
response.write(answer)
}
response.end()
})
}).listen(port, function() {
console.log('\n--------- server listening at ' + port + ' ---------')
console.log(' Returning', answer ? answer : 'nothing', 'with status code', statusCode, '\n')
})