How to close stdin in Node?

I study Node.js and thought that I had a very simple script, but not about customization, it would never make my script hang forever.

Let's say that I have a dumb server running:

$ nc -l 32001 <<EOF
HTTP/1.1 200 OK
Content-Type: text/plain

Works
EOF

And I run the following script:

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).on('end', () => process.exit(exitCode));
});
req.on('error', error => console.error(error));
process.stdin.pipe(req)
  .on('end', () => console.log('this does trigger'));

But when I do the following:

$ echo foobar | node my-script.js

It just hangs and never reaches the request callback. I expect the req thread to complete and the http.request callback will be called, and then it will Worksexit and finally exit the process.

I checked that the event was endactually called from process.stdin.pipe(), and I tried to manually terminate the thread reqin the callback end. But that just won't end. How do I pass stdin to http.request and still end the stream?

+4
source share
1

; . ( , ):

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
+2

Source: https://habr.com/ru/post/1694013/


All Articles