var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // здесь точно уже есть данные
}
})
This module is used for writing unit tests for your applications, you can access it with require('assert').
var path = 'public/uploads/file.txt',
buffer = new Buffer("some content\n");
fs.open(path, 'w', function(err, fd) {
if (err) {
throw 'error opening file: ' + err;
}
fs.write(fd, buffer, 0, buffer.length, null, function(err) {
if (err) throw 'error writing file: ' + err;
fs.close(fd, function() {
console.log('file written');
})
});
});
fs.open(filepath, 'r', function(err, fd) {
fs.fstat(fd, function(err, stats) {
var bufferSize=stats.size,
chunkSize=512,
buffer=new Buffer(bufferSize),
bytesRead = 0;
while (bytesRead < bufferSize) {
if ((bytesRead + chunkSize) > bufferSize) {
chunkSize = (bufferSize - bytesRead);
}
fs.read(fd, buffer, bytesRead, chunkSize, bytesRead);
bytesRead += chunkSize;
}
console.log(buffer.toString('utf8', 0, bufferSize));
fs.close(fd);
});
});
// Convert a binary buffer into a "binary" encoded string.
function binaryEncode(buffer) {
var string = "";
for (var i = 0, l = buffer.length; i < l; i++) {
string += String.fromCharCode(buffer[i]);
}
return string
}
// Convert a "binary" encoded string into a binary buffer.
function binaryDecode(string) {
var length = string.length;
var buffer = new Buffer(length);
for (var i = 0; i < length; i++) {
buffer[i] = string.charCodeAt(i);
}
}
client.on('stream', function(stream, meta){
var bufs = [];
stream.on('data', function(data){
bufs.push(data);
});
stream.on('end', function(){
var buf = binaryEncode(Buffer.concat(bufs));
fs.writeFile(__dirname + "/tmp/"+meta.frame+".dat", buf, "binary", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
});
}
Как это верно все сделать?
И чтобы если нагрузка на один повысится не падали другие
list: function(callback) {
pool.getConnection(function(err, connection) {
connection.query( 'SELECT * FROM tasks', function(err, rows) {
callback(rows);
connection.release();
});
});
},
var tasks = require('./tasks');
var list = tasks.list(function (rows) {
console.log(rows);
});
checkPrice(items).then(console.log.bind(console, "Call result: "));
function checkPrice(items) {
return Promise.all(items.map(function(item, index, array) {
return new Promise(function(resolve) {
market.getItemsPrice(730, item.market_hash_name, function(data) {
var price = parseFloat(data[item.market_hash_name].lowest_price.replace("$",""));
resolve(price);
console.log("Function result: " + price);
});
});
})).then(function(prices) {
//сумма всех полученных цен
return prices.reduce(function(result, price) {
result += price;
return result;
}, 0);
});
}