1
每當我嘗試打印字符串時,執行都會暫停並退出程序。嘗試打印字符串時,代碼突然停止執行
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);
這裏是一個測試客戶端,cosumes上述服務。
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();
你可以insted嘗試'process.stdout.write(s);' –
嘗試了上面...同樣的結果:( – deostroll
嘗試'console.log('String:',s);' –