Skip to content

Instantly share code, notes, and snippets.

@guptahitesh121
Last active July 28, 2020 16:19
Show Gist options
  • Save guptahitesh121/ec8071bb5c57a2d30acc1b5874d05612 to your computer and use it in GitHub Desktop.
Save guptahitesh121/ec8071bb5c57a2d30acc1b5874d05612 to your computer and use it in GitHub Desktop.
A Node js server which receives network request and response information from the client and saves it in files. Files are named in such a way that it is easier to find a request and response information of a particular http request.
const express = require('express');
var bodyParser = require('body-parser');
var fs = require("fs");
const app = express();
const port = 3000;
app.use(bodyParser.json({ limit: '50mb' }));
app.listen(port, () => console.log(`Logger Proxy Server is listening on port ${port}`));
const LogPath = 'C:\\Users\\baxture\\Desktop\\LOGS';
app.post('/', function (request, response) {
dump(request.body);
response.send('done');
});
function dump(data) {
if (data == null) return;
var status = data.status != null ? data.status : 0;
var url = data.url.split('\/');
url = url.slice(3, url.length).join('-');
var url = url.replace(/[^0-9a-zA-Z]/g, '-');
var fileName;
if (status > 0) {
fileName = `${LogPath}\\${data.method.toUpperCase()}-${data.type.toUpperCase()}-${status}-${url}.txt`;
} else {
fileName = `${LogPath}\\${data.method.toUpperCase()}-${data.type.toUpperCase()}-${url}.txt`;
}
var text = `${data.method.toUpperCase()} : ${data.url}\n\n`;
if (status > 0) {
text += `STATUS : ${status}\n\n`;
}
text += `HEADERS : ${JSON.stringify(data.headers, null, ' ')}\n\n`;
text += `${data.type.toUpperCase()} BODY : ${JSON.stringify(data.body, null, ' ')}`;
fs.writeFile(fileName, text, function (err) {
if (err) {
return console.error(err);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment