Created
August 15, 2019 18:28
-
-
Save jdecaron/ffc0e55a290ebf9badfdf0913f5add8a to your computer and use it in GitHub Desktop.
Change public Gist that dumps HTTP request and print direct to console instead of writing into file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let fs = require('fs'), | |
http = require('http'), | |
HOSTNAME =process.env.HOSTNAME || '0.0.0.0', | |
PORT = process.env.PORT || 8080, | |
ACK_HTTP_CODE = 200, | |
ACK_CONTENT_TYPE = 'text/plain', | |
ACK_MESSAGE = 'HELLO'; | |
{ | |
let server = http.createServer((request,response) => { | |
let requestDataSlabList = [], | |
httpMethod = request.method.toUpperCase(), | |
requestURI = request.url; | |
console.log(`Incoming request\nMethod: ${httpMethod}\nURI: ${requestURI}`); | |
request.on('data',(data) => { | |
requestDataSlabList.push(data); | |
}); | |
request.on('end',(data) => { | |
response.writeHead( | |
ACK_HTTP_CODE, | |
{'Content-Type': ACK_CONTENT_TYPE} | |
); | |
response.end(`${ACK_MESSAGE}\n`); | |
let headerItemList = [], | |
dataSlab = requestDataSlabList.join(''); | |
for (let headerItem of Object.keys(request.headers).sort()) { | |
headerItemList.push(`\t${headerItem}: ${request.headers[headerItem]}`); | |
} | |
console.log(`Method: ${httpMethod}\nURI: ${requestURI}\n` + | |
`Headers:\n${headerItemList.join('\n')}\n\n${dataSlab}\n\n\n`) | |
}); | |
}); | |
console.log(`Listening on ${HOSTNAME}:${PORT}\n`); | |
server.listen(PORT,HOSTNAME); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment