文字列を印刷しようとすると、実行が中断してプログラムが終了します。文字列の印刷中にコードが実行を突然停止する
var config = require('../config.json');
var upacConfig = config.upac;
var endpoint = upacConfig.endpoint;
var customer = upacConfig.customer;
var port = upacConfig.port;
const http = require('http');
const util = require('util');
module.exports.getAll = function getAll(cb) {
var url = "/" + upacConfig.methods.all + "?customer=" + customer;
console.log(url);
http.request({
host: endpoint,
path: url,
port: port
}, function(resp) {
let s = '';
// console.log('response');
// var lines = 0;
resp.on('data', function(d){
s += d.toString();
// console.log(++lines);
})
.on('end', function(){
console.log(typeof s);
console.log('length:', s.length);
console.log(s); // <-- problem! lines below are not executed...
console.log('length:', s.length);
cb(null, s);
})
})
.on('error', e => {
console.log('Err:', e);
})
.end();
};
ノードのバージョン:7.1.0
次のされたすべてのコードを出力することを:
/api/v1/all?customer=foo
string
length: 323416
編集:MVEについては、次のようにします。ここでは
は簡単ですサーバーは、上記の文字数のテキストファイルを提供します。 (私のJSON文字列は長かったです...)。
const http = require('http');
const fs = require('fs');
const path = require('path');
var file = path.join(__dirname,'json.txt')
var app = http.createServer(function(req, res) {
fs.createReadStream(file).pipe(res);
});
app.listen(3000);
ここには、aboveが何を提供するかを示すテストクライアントがあります。
const http = require('http');
http.request('http://localhost:3000/', r => {
var s = '';
r.on('data', d => s+= d)
.on('end',() => {
console.log('length:', s.length);
console.log(s);
});
}).end();
あなたは 'process.stdout.write(s);を試してみることができますか? –
は上記を試しました...同じ結果:( – deostroll
' console.log( 'String:'、s); ' –