Skip to content

Commit 1aae698

Browse files
committed
30 days of node tutorial series code snippets
1 parent 8b1fa80 commit 1aae698

25 files changed

Lines changed: 850 additions & 0 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var cp = require('child_process');
2+
3+
for(var i=1; i<6; i++) {
4+
var workerProcess = cp.exec('node slave.js ' + i , function(error, stdout, stderr) {
5+
if (error) {
6+
console.log(error.stack);
7+
console.log('Error Code: '+error.code);
8+
console.log('Error Signal: '+error.signal);
9+
}
10+
11+
if(stderr){
12+
console.log('value of stderr: ' + stderr);
13+
}
14+
15+
console.log('Value of stdout: ' + stdout);
16+
17+
18+
});
19+
workerProcess.on('exit', function (code) {
20+
//console.log("exit code : "+ code);
21+
console.log('Child process exited ');
22+
});
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var ef = require('child_process').execFile;
2+
var child = ef('node', ['--version'], (err, stdout, stderr) => {
3+
if (err) {
4+
console.log('stderr', stderr);
5+
throw err;
6+
}
7+
console.log('Node.js version : ', stdout);
8+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var cp = require('child_process');
2+
3+
for(var i=1; i<6; i++) {
4+
var worker = cp.fork("slave.js", [i]);
5+
6+
worker.on('close', function (code) {
7+
console.log('child process exited with code ' + code);
8+
});
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var cp = require('child_process');
2+
3+
for(var i = 1; i<6; i++) {
4+
var worker = cp.spawn('node', ['slave.js', i]);
5+
6+
worker.stdout.on('data', function (data) {
7+
console.log('Value of Stdout : ' + data);
8+
});
9+
10+
worker.stderr.on('data', function (data) {
11+
console.log('stderr: ' + data);
12+
});
13+
14+
worker.on('close', function (code) {
15+
//console.log("Exit code : " + code);
16+
console.log('child process exited with code ');
17+
});
18+
}

day24-child-processes/slave.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Child Process number " + process.argv[2] + " is executed." );

day27-assert/assert-1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//Name of the file : assert-1.js
2+
3+
var assert = require('assert');
4+
var a = 10;
5+
var b = 20;
6+
assert(a > b);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var assert = require('assert');
2+
function demo (x,y,z) {
3+
var value = x + y + z ;
4+
return value;
5+
}
6+
var output = demo(3,2,10);
7+
console.log("Output : " + output);
8+
var expected_output = 12;
9+
console.log("Expected Output : " + expected_output);
10+
assert( output === expected_output , 'This is not what we expected');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var assert = require('assert');
2+
function demo (x,y,z) {
3+
var value = x + y + z ;
4+
return value;
5+
}
6+
var output = demo(3,2,10);
7+
console.log("Output : " + output);
8+
var expected_output = 15;
9+
console.log("Expected Output : " + expected_output);
10+
assert( output === expected_output , 'This is not what we expected');

day27-assert/assert.deepEqual.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//Name of the file : assert.deepEqual.js
2+
var assert = require('assert');
3+
var a = 10;
4+
var b = '10';
5+
var c = 10.25;
6+
//Case 1
7+
assert.deepEqual(a,b, "Nothing printed because they are using == for comparison");
8+
//Case 2
9+
assert.deepEqual(a,c, "Error because values doesn't match");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//Name of the file : assert.deepStrictEqual.js
2+
var assert = require('assert');
3+
var a = 10;
4+
var b = '10';
5+
6+
assert.deepStrictEqual(a,b, "Error because they are using === for comparison");

0 commit comments

Comments
 (0)