Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Chapter 5

  • Loading branch information...
commit 64fc614ae789fe3777fcc0de18efe0cc597211c9 1 parent db61475
@Hendrik authored
View
19 05_ChildProc_FS/childprocess.js
@@ -0,0 +1,19 @@
+var sys = require("sys"),
+ filename = process.ARGV[2];
+
+if (!filename)
+ return sys.puts("Usage: node " + __filename.replace(__dirname + "/", "") + " filename");
+
+// create child process and add a listener for its "output" event
+var tail = require("child_process").spawn("tail", ["-f", filename]);
+tail.addListener("output", function(data) {
+ sys.puts(data);
+});
+tail.addListener("exit", function(code) {
+ sys.puts("Child process stopped with exit code: " + code);
+});
+
+// add a timer to kill the child proces after 10 seconds
+setTimeout(function() {
+ tail.kill();
+}, 10000);
View
7 05_ChildProc_FS/childprocess_2.js
@@ -0,0 +1,7 @@
+var sys = require("sys"),
+ exec = require("child_process").exec;
+
+exec("ls /", function (err, stdout, stderr) {
+ if (err) throw err;
+ sys.puts(stdout);
+});
View
63 05_ChildProc_FS/filesystem.js
@@ -0,0 +1,63 @@
+var sys = require("sys"),
+ path = require("path"),
+ fs = require("fs");
+
+var file = path.join(__dirname, "test_file.txt");
+
+fs.open(file, "w", 0644, function(err, fd) {
+ if (err) throw err;
+ sys.puts("File test_file.txt opened");
+ fs.write(fd, "Hello World", 0, "utf8", function(err, written) {
+ sys.puts("Data written");
+ if (err) throw err;
+ fs.closeSync(fd);
+
+ fs.watchFile(file, function(curr, prev) {
+ sys.puts("\n\ttest_file.txt has been edited");
+ sys.puts("\tThe current mtime is: " + curr.mtime);
+ sys.puts("\tThe previous mtime was: " + prev.mtime + "\n");
+ });
+
+ chmod_file();
+ });
+});
+
+function chmod_file() {
+ fs.chmod(file, 0777, function(err) {
+ if (err) throw err;
+ sys.puts("\nchmod value of test_file.txt set to 777");
+
+ fs.chmodSync(file, 0644);
+ sys.puts("chmod value of test_file.txt set to 644");
+
+ show_dir();
+ });
+}
+
+function show_dir() {
+ sys.puts("\nContent of " + __dirname + ":");
+ fs.readdir(__dirname, function(err, files) {
+ if (err) throw err;
+ sys.puts(JSON.stringify(files));
+ show_file_content();
+ });
+}
+
+function show_file_content() {
+ fs.readFile(file, function(err, content) {
+ if (err) throw err;
+ sys.puts("\nContent of test_file.txt:");
+ sys.puts(content);
+ delete_file();
+ });
+}
+
+function delete_file() {
+ fs.unwatchFile(file);
+ sys.puts("\nStopping watchFile of test_file.txt");
+
+ fs.unlink(file, function(err) {
+ if (err) throw err;
+ sys.puts("\ntest_file.txt has been deleted.");
+ });
+}
Please sign in to comment.
Something went wrong with that request. Please try again.