Guest User

Request example

a guest
Feb 21st, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http = require('http');
  2. var querystring = require('querystring');
  3. const { performance } = require('perf_hooks');
  4.  
  5. const t1 = performance.now();
  6. var post_data = querystring.stringify({});
  7.  
  8. // An object of options to indicate where to post to
  9. var post_options = {
  10.     host: 'localhost',
  11.     port: '3000',
  12.     path: '/',
  13.     method: 'POST',
  14.     headers: {
  15.         'Content-Type': 'application/json;charset=utf-8',
  16.         'Content-Length': Buffer.byteLength(post_data),
  17.     },
  18. };
  19.  
  20. const post_req = http
  21.     .request(post_options, (resp) => {
  22.         resp.setEncoding('utf8');
  23.         let data = '';
  24.  
  25.         // A chunk of data has been received.
  26.         resp.on('data', (chunk) => {
  27.             data += chunk;
  28.         });
  29.  
  30.         resp.on('end', () => {
  31.             const t2 = performance.now();
  32.             console.log('duration', t2 - t1);
  33.  
  34.             console.log('on end', data);
  35.         });
  36.     })
  37.     .on('error', (err) => {
  38.         console.log('Error: ' + err.message);
  39.     });
  40.  
  41. post_req.write(post_data);
  42. post_req.end();
  43.  
Advertisement
Add Comment
Please, Sign In to add comment