File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed
Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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();
Original file line number Diff line number Diff line change 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..." ) ;
You can’t perform that action at this time.
0 commit comments