File size: 884 Bytes
8925c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const logFile = path.join(__dirname, 'api_response.json');

const url = 'http://localhost:5002/api/products?page=1&limit=10&search=';

console.log('START FETCH');

http.get(url, (res) => {
  let data = '';
  
  res.on('data', (chunk) => {
    data += chunk;
  });

  res.on('end', () => {
    console.log('Response Status:', res.statusCode);
    console.log('Response Body:', data);
    try {
      fs.writeFileSync(logFile, data);
      console.log('Wrote response to:', logFile);
    } catch (err) {
      console.error('Failed to write file:', err);
    }
    console.log('END FETCH');
  });

}).on('error', (err) => {
  console.error('Error fetching API:', err.message);
});