Skip to content

Commit f9731fc

Browse files
committed
Add autoreload example
1 parent e06e67c commit f9731fc

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

JavaScript/6-watch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fs = require('fs');
44

55
const watch = (path) => {
6-
const watcher = fs.watch(path, (event, file) => {
6+
fs.watch(path, (event, file) => {
77
console.dir({ event, file });
88
});
99
};

JavaScript/7-autoreload.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
let buffer;
6+
7+
const load = (path) => {
8+
fs.readFile(path, (err, data) => {
9+
if (err) throw err;
10+
buffer = data;
11+
console.log('\x1Bc');
12+
console.log('Buffer length: ' + buffer.length);
13+
console.log(buffer.toString());
14+
});
15+
};
16+
17+
const watch = (path) => {
18+
fs.watch(path, (event, file) => {
19+
console.dir({ event, file });
20+
load(path);
21+
});
22+
};
23+
24+
const path = './1-readFileSync.js';
25+
load(path);
26+
watch(path);

0 commit comments

Comments
 (0)