Skip to content

Instantly share code, notes, and snippets.

@victor9000
Forked from magnetikonline/README.md
Created January 16, 2018 20:37
Show Gist options
  • Save victor9000/5831f535366cb3bf0f4f0779f6a2eba7 to your computer and use it in GitHub Desktop.
Save victor9000/5831f535366cb3bf0f4f0779f6a2eba7 to your computer and use it in GitHub Desktop.
Node.js HTTP receiving request dump server.

Node.js HTTP receiving request dump server

Simple HTTP server receiving requests and dumping to flat file.

Usage

Start the listening HTTP server:

root# nodejs ./httprequestdump.js
Listening on 0.0.0.0:8080

Now make a curl request:

root# curl --data-urlencode "param1=value" --data-urlencode "param2=value" --verbose http://127.0.0.1:8080/my/path/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> POST /my/path/ HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8080
> Accept: */*
> Content-Length: 25
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 25 out of 25 bytes
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Fri, 27 May 2016 05:37:56 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
<
HELLO
* Connection #0 to host 127.0.0.1 left intact

And view the response:

Incoming request
Method: POST
URI: /my/path/
End of request, 25 bytes received.

root# cat dumpdata.txt
Method: POST
URI: /my/path/
Headers:
	accept: */*
	content-length: 25
	content-type: application/x-www-form-urlencoded
	host: 127.0.0.1:8080
	user-agent: curl/7.35.0

param1=value&param2=value
'use strict';
let fs = require('fs'),
http = require('http'),
LISTEN_HOSTNAME = '0.0.0.0',
LISTEN_PORT = 8080,
DUMP_FILE_PATH = 'dumpdata.txt',
ACK_HTTP_CODE = 200,
ACK_CONTENT_TYPE = 'text/plain',
ACK_MESSAGE = 'HELLO';
{
let server = http.createServer((request,response) => {
// start of new HTTP request
let requestDataSlabList = [],
httpMethod = request.method.toUpperCase(),
requestURI = request.url;
// summary request details
console.log(`Incoming request\nMethod: ${httpMethod}\nURI: ${requestURI}`);
// wire up request events
request.on('data',(data) => {
// add received data to buffer
requestDataSlabList.push(data);
});
request.on('end',(data) => {
// send response to client
response.writeHead(
ACK_HTTP_CODE,
{'Content-Type': ACK_CONTENT_TYPE}
);
response.end(`${ACK_MESSAGE}\n`);
// write/append received request to file
let headerItemList = [],
dataSlab = requestDataSlabList.join('');
for (let headerItem of Object.keys(request.headers).sort()) {
headerItemList.push(`\t${headerItem}: ${request.headers[headerItem]}`);
}
fs.appendFile(
DUMP_FILE_PATH,
`Method: ${httpMethod}\nURI: ${requestURI}\n` +
`Headers:\n${headerItemList.join('\n')}\n\n${dataSlab}\n\n\n`,
(err) => {
// end of HTTP request
console.log(`End of request, ${dataSlab.length} bytes received.\n`);
}
);
});
});
// start listening server
console.log(`Listening on ${LISTEN_HOSTNAME}:${LISTEN_PORT}\n`);
server.listen(LISTEN_PORT,LISTEN_HOSTNAME);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment