相关疑难解决方法(0)

Stdin read fails on some input

#!/usr/bin/env node

function stdinReadSync() {
   var b = new Buffer(1024);
   var data = '';

   while (true) {
      var n = require('fs').readSync(process.stdin.fd, b, 0, b.length);
      if (!n) break;
      data += b.toString(null, 0, n);
   }
   return data;
}

var s = stdinReadSync();
console.log(s.length);
Run Code Online (Sandbox Code Playgroud)

The above code (taken from Stackoverflow) works just fine if you feed it with echo, cat, ls, but will fail with curl output.

$ echo abc | ./test.js
4

$ ls | ./test.js
1056

$ cat …
Run Code Online (Sandbox Code Playgroud)

stdin node.js

2
推荐指数
1
解决办法
415
查看次数

标签 统计

node.js ×1

stdin ×1