-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathservices.js
More file actions
35 lines (31 loc) · 767 Bytes
/
services.js
File metadata and controls
35 lines (31 loc) · 767 Bytes
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
'use strict';
const fs = require('node:fs').promises;
const personGetService = async () => {
try {
const data = await fs.readFile('./person.json');
const obj = JSON.parse(data);
obj.birth = new Date(obj.birth);
const difference = new Date() - obj.birth;
obj.age = Math.floor(difference / 31536000000);
delete obj.birth;
const sobj = JSON.stringify(obj);
return sobj;
} catch (e) {
return false;
}
}
const personPostService = async (obj) => {
try {
if (obj.name) obj.name = obj.name.trim();
const data = JSON.stringify(obj);
await fs.writeFile('./person.json', data);
return data;
} catch (e) {
console.error(e);
return false;
}
}
module.exports = {
personGetService,
personPostService
}