Skip to content

Instantly share code, notes, and snippets.

@sposmen
Last active June 8, 2022 20:33
Show Gist options
  • Save sposmen/a64b5348864d4f190a9656fdc6a543ee to your computer and use it in GitHub Desktop.
Save sposmen/a64b5348864d4f190a9656fdc6a543ee to your computer and use it in GitHub Desktop.
Node Server Body Parser

Run

node server_body_parser.js

Setup in your endpoint

http://localhost:8888

Change port

Change the variable port in the code. 8888 is the default

Result examples

GET to http://localhost:8888/?test=%22This%20is%20the%20test%20GET%20request%22 through browser

Result

Request:  {
  timestamp: 1654719925834,
  rawHeaders: [
    'Host',
    'localhost:8888',
    'Connection',
    'keep-alive',
    'sec-ch-ua',
    '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
    'sec-ch-ua-mobile',
    '?0',
    'sec-ch-ua-platform',
    '"macOS"',
    'Upgrade-Insecure-Requests',
    '1',
    'User-Agent',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36',
    'Accept',
    'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Sec-Fetch-Site',
    'none',
    'Sec-Fetch-Mode',
    'navigate',
    'Sec-Fetch-User',
    '?1',
    'Sec-Fetch-Dest',
    'document',
    'Accept-Encoding',
    'gzip, deflate, br',
    'Accept-Language',
    'en-US,en;q=0.9',
    'Cookie',
    '_ga=GA1.1.1954339217.1648837934; PGADMIN_LANGUAGE=en; _ga_1TLVNCHFCR=GS1.1.1652998450.3.0.1653000194.0'
  ],
  httpVersion: '1.1',
  method: 'GET',
  remoteAddress: '::1',
  remoteFamily: 'IPv6',
  url: '/?test=%22This%20is%20the%20test%20GET%20request%22'
}
Body:

POST to http://localhost:8888 through Insomnia client with Bearer emulated token

{
	"test": "This is the POST test"
}

Result

Request:  {
  timestamp: 1654720197672,
  rawHeaders: [
    'Host',
    'localhost:8888',
    'User-Agent',
    'insomnia/2022.3.0',
    'Content-Type',
    'application/json',
    'Authorization',
    'Bearer 123456',
    'Accept',
    '*/*',
    'Content-Length',
    '36'
  ],
  httpVersion: '1.1',
  method: 'POST',
  remoteAddress: '::ffff:127.0.0.1',
  remoteFamily: 'IPv6',
  url: '/'
}
Body: { test: 'This is the POST test' }
/*
* This small server is to show in console any kind of request data
* being used for Check Webhooks/API requests, etc
* To start the server run `node server_body_parser.js`
* point your endpoint to http://localhost:8888
* */
const http = require("http");
const port = 8888;
/* Edit this method to set the response you need */
const expectedReturn = (response) => {
response.statusCode = 200
response.setHeader('Content-Type', 'application/json');
response.end("{}");
}
const showRequestBase = (
{
rawHeaders,
httpVersion,
method,
socket: { remoteAddress, remoteFamily },
url
}
) => {
console.log("Request: ",
{
timestamp: Date.now(),
rawHeaders,
httpVersion,
method,
remoteAddress,
remoteFamily,
url
}
);
}
const processBody = (body) => (chunk) => body.push(chunk);
const endBody = (body, response) => (
() => {
body = Buffer.concat(body);
showBody(body.toString());
expectedReturn(response)
}
);
const showBody = (bodyRaw)=>{
const body = ((()=>{
try {
return JSON.parse(bodyRaw);
} catch (e) {
return bodyRaw;
}
}))();
console.log("Body:" , body);
}
const onRequestError = (error) => console.error(error.message);
const server = http.createServer((request, response) => {
let body = [];
showRequestBase(request)
request.on("data", processBody(body));
request.on("end", endBody(body, response));
request.on("error", onRequestError);
});
server.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment