-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.mjs
More file actions
58 lines (56 loc) · 1.45 KB
/
callback.mjs
File metadata and controls
58 lines (56 loc) · 1.45 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import fs from "node:fs";
//FILE LOGGER
const content='node js is a fun but i am still having problem with the low level fs methods mostly things partaining POSIX'
const directory='/content/subfolder/logger.txt'
function closeFd(fd) {
fs.close(fd, (err) => {
if (err) throw err;
})
}
fs.open(directory,(err,dd)=>{
const fd=dd
if (err) {
if (err.code==='ENOENT') {
fs.writeFile(directory,content,(err)=>console.log(`unable to writeFile ${err}`))
closeFd(fd)
}else{
throw err
}
}
closeFd(fd)
})
async function appending(path) {
const timestamp= await time()
fs.open(path,(err,fd)=>{
if(!err){
try {
fs.appendFile(fd,timestamp,'utf8',(err)=>{
if(err){
closeFd(fd)
throw err
}
console.log('appending sucess');
closeFd(fd)
})
} catch (err) {
closeFd(fd)
throw err
}
}else{
closeFd(fd)
console.log(`file missing ${err}`);
}
})
}
async function time() {
const timestamp=`log entry at: ${Date.now()}/n`
return timestamp
}
appending(directory)
fs.readFile(directory,(err,data)=>{
if(err){
throw err
}else{
console.log(data)
}
})