-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
41 lines (35 loc) · 1004 Bytes
/
utils.js
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
38
39
40
41
const fs = require("fs");
function writeDataToFile(fileName, content) {
fs.writeFile(fileName, JSON.stringify(content), "utf8", (err) => {
if (err) {
console.log(err);
}
});
}
function getPostData(req) {
console.log('getPostData (12):', req); //getPostData (12): { url: '/api/products/id/1', method: 'PUT', data: { price: 89.99 } } eg, but THROWS on PUT
return new Promise((resolve, reject) => {
try {
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
req.on('end', () => {
resolve(body);
});
} catch (error) {
//reject(error)
console.log("\nI think I'm trying something weird and circular here.\nLike handling the own response after having bypassed the handling")
}
});
}
let DEBUG = true; // or false
function customLog(message) {
if (DEBUG) {
console.log(message);
}
}
function setDebugMode(value) {
DEBUG = value;
}
module.exports = { writeDataToFile, getPostData, customLog, setDebugMode };