Advertisement
Pencheff

Simple NodeJS http server logging post requests

Aug 19th, 2022 (edited)
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http = require("http");
  2. const qs = require('querystring');
  3.  
  4. const host = '0.0.0.0';
  5. const port = 8000;
  6.  
  7. const requestListener = function (req, res) {
  8.   if (req.method == 'POST') {
  9.     let body = '';
  10.     req.on('data', function (data) {
  11.         body += data;
  12.         if (body.length > 1e6) {
  13.             req.connection.destroy();
  14.         }
  15.     });
  16.     req.on('end', function () {
  17.         let postData = qs.parse(body);
  18.         // use POST
  19.         console.log(new Date(), postData);
  20.     });
  21.   }
  22.  
  23.   res.writeHead(200);
  24.   res.end("OK");
  25. };
  26.  
  27. const server = http.createServer(requestListener);
  28. server.listen(port, host, () => {
  29.   console.log(`Server is running on http://${host}:${port}`);
  30. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement