Skip to content

Instantly share code, notes, and snippets.

@umutbasal
Created September 24, 2019 12:51
Show Gist options
  • Save umutbasal/e027fbb2184177c0f63fcc0a0615a82d to your computer and use it in GitHub Desktop.
Save umutbasal/e027fbb2184177c0f63fcc0a0615a82d to your computer and use it in GitHub Desktop.
const http = require('http')
const port = 80
const fs = require('fs')
const requests = {data:[]};
const requestHandler = (request, response) => {
let data = []
request.on('data', chunk => {
data.push(chunk)
});
request.on('end', () => {
requests.data.push(JSON.stringify({headers: request.headers, method: request.method, url: request.url, body: data.toString()}));
})
console.log(requests)
response.setHeader('Content-Type', 'text/html');
response.end(`Hello Node.js Server!`)
if (request.url==("/SAVETHISSHIT")){
file = JSON.stringify(requests)
fs.writeFile('all_requests.json', file, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
response.end(`YOU'VE SAVED IT`)
});
}
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment