How random is crypto#randomBytes?

How random is crypto.randomBytes()? Usually, random enough for whatever purpose you need. crypto.randomBytes() generates cryptographically secure random data: crypto.randomBytes(size[, callback]) Generates cryptographically strong pseudo-random data. The size argument is a number indicating the number of bytes to generate. This means that the random data is secure enough to use for encryption purposes. In fact, the … Read more

Testing asynchronous function with mocha

You have to specify the callback done as the argument to the function which is provided to mocha – in this case the it() function. Like so: describe(‘api’, function() { it(‘should load a user’, function(done) { // added “done” as parameter assert.doesNotThrow(function() { doRequest(options, function(res) { assert.equal(res, ‘{Object … }’); // will not fail assert.doesNotThrow … Read more

In Node.js / Express, how do I “download” a page and gets its HTML?

var util = require(“util”), http = require(“http”); var options = { host: “www.google.com”, port: 80, path: “https://stackoverflow.com/” }; var content = “”; var req = http.request(options, function(res) { res.setEncoding(“utf8”); res.on(“data”, function (chunk) { content += chunk; }); res.on(“end”, function () { util.log(content); }); }); req.end();

socket.io private message

No tutorial needed. The Socket.IO FAQ is pretty straightforward on this one: socket.emit(‘news’, { hello: ‘world’ }); EDIT: Folks are linking to this question when asking about how to get that socket object later. There is no need to. When a new client connects, save a reference to that socket on whatever object you’re keeping … Read more

How to chain a variable number of promises in Q, in order?

There’s a nice clean way to to this with [].reduce. var chain = itemsToProcess.reduce(function (previous, item) { return previous.then(function (previousValue) { // do what you want with previous value // return your async operation return Q.delay(100); }) }, Q.resolve(/* set the first “previousValue” here */)); chain.then(function (lastResult) { // … }); reduce iterates through the … Read more

Run shell script with node.js (childProcess)

The exec function callback has error, stdout and stderr arguments passed to it. See if they can help you diagnose the problem by spitting them out to the console: exec(‘~/./play.sh /media/external/’ + req.params.movie, function (error, stdout, stderr) { console.log(‘stdout: ‘ + stdout); console.log(‘stderr: ‘ + stderr); if (error !== null) { console.log(‘exec error: ‘ + … Read more