@2012-6-12 @night of node
forEach()
for (var prop in object)
for (var i = 0; i < length; i++) and while
for (var i = 0; i < data.length; i++) {
//do something
}
for (var i = 0, l = data.length; i < l; i++) {
//do something
}
get(params, function(err, data) {
if (err) {
callback(err);
}
//对data进行操作
var row = data[0];
});
function asyncfun(data, callback) {
try {
callback(null, JSON.parse(data.toString()));
} catch (e) {
callback(e);
}
}
var json = {'a': 'b'};
var jsonstr = JSON.stringify(json);
var d = new Buffer(jsonstr);
asyncfun(d, function(err, data) {
console.log(err);
throw new Error('new Error');
});
null
[Error: new Error]
var data = "";
res.on('data', function (chunk) {
data += chunk;
})
.on("end", function () {
});
var chunks = [];
var size = 0;
res.on('data', function (chunk) {
chunks.push(chunk);
size += chunk.length;
});
res.on('end', function () {
var data = new Buffer(size);
for (var i = 0, pos = 0, l = chunks.length; i < l; i++) {
var chunk = chunks[i];
chunk.copy(data, pos);
pos += chunk.length;
}
});
res.on('end', function () {
var data = null;
switch(chunks.length) {
case 0: data = new Buffer(0);
break;
case 1: data = chunks[0];
break;
default:
data = new Buffer(size);
for (var i = 0, pos = 0, l = chunks.length; i < l; i++) {
var chunk = chunks[i];
chunk.copy(data, pos);
pos += chunk.length;
}
break;
}
});
func1(err, function(err1, data1) {
func2(err1, function(err2, data2) {
func3(err3, function(err3, data3) {
func4(err4, function(err4, data4) {
.......
})
})
})
})
var options = {
host: 'www.google.com',
port: 80,
path: '/',
};
http.get(options, function(res) {
JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
app.get('/', function () {
// to get buffer1 and buffer2;
res.write(buffer1);
res.write(buffer2);
res.end();
})
解决方案
app.get('/', function () {
// to get buffer1 and buffer2;
var buff = combine(buffer1, buffer2);
res.end(buff);
})
/
#