Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active September 21, 2022 05:34
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save magnetikonline/dbcd81e58311fd432f5979860eee9c91 to your computer and use it in GitHub Desktop.
Save magnetikonline/dbcd81e58311fd432f5979860eee9c91 to your computer and use it in GitHub Desktop.
Node.js HTTP receiving request dump server.

Node.js HTTP receiving request dump server

HTTP server which receives requests and dumps them to a flat file.

Usage

Start HTTP server:

$ nodejs ./httprequestdump.js
Listening on 0.0.0.0:8080

Now make a curl request:

$ 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

Then view the captured response:

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

$ 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';
const 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';
{
const server = http.createServer((request,response) => {
// start of new HTTP request
const 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,
{
'Access-Control-Allow-Origin': '*',
'Content-Type': ACK_CONTENT_TYPE
}
);
response.end(`${ACK_MESSAGE}\n`);
// write/append received request to file
const headerItemList = [],
dataSlab = requestDataSlabList.join('');
for (const 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);
}
@osbre
Copy link

osbre commented Oct 28, 2020

BTW you can also solve CORS by using this block instead:

            response.writeHead(
                ACK_HTTP_CODE,
                {
                    'Content-Type': ACK_CONTENT_TYPE,
                    'Access-Control-Allow-Origin': '*'
                }
            );

(use this on line 37-40)

@magnetikonline
Copy link
Author

Thanks @osbre - worthwhile addition - added! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment