; . ( , ):
res.pipe(process.stdout).on('end', () => process.exit(exitCode));
: " stdout, stdout, ".
: " stdout. , ". :
res.pipe(process.stdout);
res.on('end', () => process.exit(exitCode));
, process.stdout, / . response , HTTP- .
:
process.stdin.pipe(req)
.on('end', () => console.log('this does trigger'));
: " stdin, , , ".
: " stdin, . stdin , ". :
process.stdin.pipe(req);
process.stdin.on('end', () => console.log('this does trigger'));
, stdin end finish:
process.stdin.pipe(req).on('finish', () => console.log('Request has finished writing/sending');
process.stdin.on('end', () => console.log('Stdin has no more data'));
, :
var http = require('http');
var options = {
hostname: 'localhost',
port: 32001,
method: 'POST',
headers: {'Content-Type': 'text/plain'}
};
var req = http.request(options, res => {
var exitCode = res.statusCode >= 200 && res.statusCode < 300 ? 0 : 1;
res.pipe(process.stdout);
res.on('end', () => {
console.log('Response (IncomingMessage) has no more data; exiting with code:', exitCode);
process.exit(exitCode);
});
});
req.on('error', error => console.error(error));
process.stdin.on('end', () => console.log('Stdin has no more data.'));
process.stdin.pipe(req).on('finish', () => console.log('Request has finished writing/sending'));
:
$ echo Some text from stdin | node test.js; echo $?
Stdin has no more data.
Request has finished writing/sending
Works
Response (IncomingMessage) has no more data; exiting with code: 0
0
"":
$ nc -l 32001 <<EOF
HTTP/1.1 200 OK
Content-Type: text/plain
Works
EOF
POST / HTTP/1.1
Content-Type: text/plain
Host: localhost:32001
Connection: close
Transfer-Encoding: chunked
15
Some text from stdin
0