Skip to content

Commit 2653a93

Browse files
committed
Parent child and additional changes
1 parent 7e3a579 commit 2653a93

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

parent-child/child.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var num = process.argv[2];
2+
console.log("Child process "+num+" started.");
3+
process.on('exit',function(){
4+
console.log("Process "+num+" ended.");
5+
});

parent-child/parent.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var child_process = require("child_process");
2+
3+
for(var i=0;i<3;i++){
4+
// var child = child_process.exec("node child.js "+i,function(err,stdout){
5+
// if(err)
6+
// console.log(err);
7+
// else
8+
// console.log(stdout);
9+
// });
10+
11+
var child = child_process.spawn("node",['child.js',i]);
12+
child.stdout.on('data',function(stdout){
13+
console.log(stdout.toString());
14+
})
15+
16+
};
17+

server-client/client.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var http = require("http");
2+
3+
var options = {
4+
'host':'127.0.0.1',
5+
'port':'1234',
6+
'path':'/new.txt'
7+
}
8+
9+
var callback = function(response){
10+
11+
var data = "";
12+
13+
response.on('data',function(val){
14+
data+=val;
15+
});
16+
response.on('end',function(){
17+
console.log(data);
18+
});
19+
};
20+
21+
var req = http.request(options,callback);
22+
req.end();
23+
24+
// var http = require('http');
25+
26+
// // Options to be used by request
27+
// var options = {
28+
// host: 'localhost',
29+
// port: '1234',
30+
// path: '/new.txt'
31+
// };
32+
33+
// // Callback function is used to deal with response
34+
// var callback = function(response){
35+
// // Continuously update stream with data
36+
// var body = '';
37+
// response.on('data', function(data) {
38+
// body += data;
39+
// });
40+
41+
// response.on('end', function() {
42+
// // Data received completely.
43+
// console.log(body);
44+
// });
45+
// }
46+
// // Make a request to the server
47+
// var req = http.request(options, callback);
48+
// req.end();

server-client/server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var http = require("http");
2+
var url = require("url");
3+
var fs = require("fs");
4+
5+
6+
http.createServer(function(req,res){
7+
var path = url.parse(req.url).pathname;
8+
path = path.substr(1);
9+
console.log("pathname requested: "+path);
10+
fs.readFile(path,function(err,data){
11+
if(err)
12+
{
13+
console.log(err.stack);
14+
res.writeHead(404,{'Content-Type':'text/html'});
15+
}
16+
else
17+
{
18+
console.log(data.toString());
19+
res.writeHead(200,{'Content-Type':'text/html'});
20+
// res.writeHead(path);
21+
res.write(data.toString());
22+
}
23+
res.end();
24+
})
25+
26+
}).listen(1234);
27+
28+
console.log("Server @ 1234...");

0 commit comments

Comments
 (0)