Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
doc: additional example edits
Additional example edits for consistency
  • Loading branch information
jasnell committed Dec 17, 2015
commit bae6728b7ec8ca87e955596c3156f0ce96a49dfa
25 changes: 12 additions & 13 deletions doc/api/child_process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ a `'message'` event on the child.
For example:

const cp = require('child_process');

var n = cp.fork(`${__dirname}/sub.js`);
const n = cp.fork(`${__dirname}/sub.js`);

n.on('message', (m) => {
console.log('PARENT got message:', m);
Expand Down Expand Up @@ -215,7 +214,7 @@ Here is an example of sending a server:
const child = require('child_process').fork('child.js');

// Open up the server object and send the handle.
var server = require('net').createServer();
const server = require('net').createServer();
server.on('connection', (socket) => {
socket.end('handled by parent');
});
Expand Down Expand Up @@ -251,7 +250,7 @@ process.
const special = require('child_process').fork('child.js', ['special']);

// Open up the server and send sockets to child
var server = require('net').createServer();
const server = require('net').createServer();
server.on('connection', (socket) => {

// if this is a VIP
Expand Down Expand Up @@ -320,7 +319,7 @@ the parent's `child.stdio[1]` is a stream, all other values in the array are
const fs = require('fs');
const child_process = require('child_process');

child = child_process.spawn('ls', {
const child = child_process.spawn('ls', {
stdio: [
0, // use parents stdin for child
'pipe', // pipe child's stdout to parent
Expand Down Expand Up @@ -380,7 +379,7 @@ callback or returning an EventEmitter).
Runs a command in a shell and buffers the output.

const exec = require('child_process').exec;
var child = exec('cat *.js bad_file | wc -l',
const child = exec('cat *.js bad_file | wc -l',
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
Expand Down Expand Up @@ -508,7 +507,7 @@ process, the default is `process.env`.
Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the exit code:

const spawn = require('child_process').spawn;
var ls = spawn('ls', ['-lh', '/usr']);
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
Expand All @@ -526,8 +525,8 @@ Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the exit cod
Example: A very elaborate way to run 'ps ax | grep ssh'

const spawn = require('child_process').spawn;
var ps = spawn('ps', ['ax']);
var grep = spawn('grep', ['ssh']);
const ps = spawn('ps', ['ax']);
const grep = spawn('grep', ['ssh']);

ps.stdout.on('data', (data) => {
grep.stdin.write(data);
Expand Down Expand Up @@ -562,7 +561,7 @@ Example: A very elaborate way to run 'ps ax | grep ssh'
Example of checking for failed exec:

const spawn = require('child_process').spawn;
var child = spawn('bad_command');
const child = spawn('bad_command');

child.on('error', (err) => {
console.log('Failed to start child process.');
Expand All @@ -588,10 +587,10 @@ file:

const fs = require('fs');
const spawn = require('child_process').spawn;
var out = fs.openSync('./out.log', 'a');
var err = fs.openSync('./out.log', 'a');
const out = fs.openSync('./out.log', 'a');
const err = fs.openSync('./out.log', 'a');

var child = spawn('prg', [], {
const child = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ]
});
Expand Down
4 changes: 2 additions & 2 deletions doc/api/cluster.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ In a worker you can also use `process.on('error')`.

Similar to the `cluster.on('exit')` event, but specific to this worker.

var worker = cluster.fork();
const worker = cluster.fork();
worker.on('exit', (code, signal) => {
if( signal ) {
console.log(`worker was killed by signal: ${signal}`);
Expand Down Expand Up @@ -612,7 +612,7 @@ A reference to the current worker object. Not available in the master process.
cluster.fork();
cluster.fork();
} else if (cluster.isWorker) {
console.log('I am worker #' + cluster.worker.id);
console.log(`I am worker #${cluster.worker.id}`);
}

## cluster.workers
Expand Down
10 changes: 5 additions & 5 deletions doc/api/console.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ directly without `require`.

Use `require('console').Console` or `console.Console` to access this class.

var Console = require('console').Console;
var Console = console.Console;
const Console = require('console').Console;
const Console = console.Console;

You can use the `Console` class to create a simple logger like `console` but
with different output streams.
Expand All @@ -29,10 +29,10 @@ Create a new `Console` by passing one or two writable stream instances.
is used for warning or error output. If `stderr` isn't passed, the warning
and error output will be sent to the `stdout`.

var output = fs.createWriteStream('./stdout.log');
var errorOutput = fs.createWriteStream('./stderr.log');
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
var logger = new Console(output, errorOutput);
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log('count: %d', count);
Expand Down
28 changes: 14 additions & 14 deletions doc/api/crypto.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ associated with the private key being set.
Example (obtaining a shared secret):

const crypto = require('crypto');
var alice = crypto.createECDH('secp256k1');
var bob = crypto.createECDH('secp256k1');
const alice = crypto.createECDH('secp256k1');
const bob = crypto.createECDH('secp256k1');

// Note: This is a shortcut way to specify one of Alice's previous private
// keys. It would be unwise to use such a predictable private key in a real
Expand All @@ -294,8 +294,8 @@ Example (obtaining a shared secret):
// Bob uses a newly generated cryptographically strong pseudorandom key pair
bob.generateKeys();

var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');

// alice_secret and bob_secret should be the same shared secret value
console.log(alice_secret === bob_secret);
Expand Down Expand Up @@ -535,13 +535,13 @@ algorithms.

Example: this program that takes the sha256 sum of a file

var filename = process.argv[2];
const filename = process.argv[2];
const crypto = require('crypto');
const fs = require('fs');

var shasum = crypto.createHash('sha256');
const shasum = crypto.createHash('sha256');

var s = fs.ReadStream(filename);
const s = fs.ReadStream(filename);
s.on('data', (d) => {
shasum.update(d);
});
Expand Down Expand Up @@ -581,7 +581,7 @@ Returns an array with the names of the supported ciphers.

Example:

var ciphers = crypto.getCiphers();
const ciphers = crypto.getCiphers();
console.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...]

## crypto.getCurves()
Expand All @@ -590,7 +590,7 @@ Returns an array with the names of the supported elliptic curves.

Example:

var curves = crypto.getCurves();
const curves = crypto.getCurves();
console.log(curves); // ['secp256k1', 'secp384r1', ...]

## crypto.getDiffieHellman(group_name)
Expand All @@ -609,14 +609,14 @@ and communication time.
Example (obtaining a shared secret):

const crypto = require('crypto');
var alice = crypto.getDiffieHellman('modp14');
var bob = crypto.getDiffieHellman('modp14');
const alice = crypto.getDiffieHellman('modp14');
const bob = crypto.getDiffieHellman('modp14');

alice.generateKeys();
bob.generateKeys();

var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');

/* alice_secret and bob_secret should be the same */
console.log(alice_secret == bob_secret);
Expand All @@ -627,7 +627,7 @@ Returns an array with the names of the supported hash algorithms.

Example:

var hashes = crypto.getHashes();
const hashes = crypto.getHashes();
console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]

## crypto.pbkdf2(password, salt, iterations, keylen[, digest], callback)
Expand Down
14 changes: 7 additions & 7 deletions doc/api/debugger.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ For example, suppose `myscript.js` looked like this:
x = 5;
setTimeout(function () {
debugger;
console.log("world");
console.log('world');
}, 1000);
console.log("hello");
console.log('hello');

Then once the debugger is run, it will break on line 4.

Expand All @@ -46,15 +46,15 @@ Then once the debugger is run, it will break on line 4.
1 x = 5;
2 setTimeout(function () {
3 debugger;
4 console.log("world");
4 console.log('world');
5 }, 1000);
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
2 setTimeout(function () {
3 debugger;
4 console.log("world");
4 console.log('world');
5 }, 1000);
6 console.log("hello");
6 console.log('hello');
debug> repl
Press Ctrl + C to leave debug repl
> x
Expand All @@ -65,9 +65,9 @@ Then once the debugger is run, it will break on line 4.
< world
break in /home/indutny/Code/git/indutny/myscript.js:5
3 debugger;
4 console.log("world");
4 console.log('world');
5 }, 1000);
6 console.log("hello");
6 console.log('hello');
7
debug> quit
%
Expand Down
10 changes: 5 additions & 5 deletions doc/api/dgram.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Datagram sockets are available through `require('dgram')`.
Important note: the behavior of [`dgram.Socket#bind()`][] has changed in v0.10
and is always asynchronous now. If you have code that looks like this:

var s = dgram.createSocket('udp4');
const s = dgram.createSocket('udp4');
s.bind(1234);
s.addMembership('224.0.0.114');

You have to change it to this:

var s = dgram.createSocket('udp4');
const s = dgram.createSocket('udp4');
s.bind(1234, () => {
s.addMembership('224.0.0.114');
});
Expand Down Expand Up @@ -94,7 +94,7 @@ Example of a UDP server listening on port 41234:

const dgram = require('dgram');

var server = dgram.createSocket('udp4');
const server = dgram.createSocket('udp4');

server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
Expand Down Expand Up @@ -188,8 +188,8 @@ be calculated with respect to [byte length][] and not the character position.
Example of sending a UDP packet to a random port on `localhost`;

const dgram = require('dgram');
var message = new Buffer('Some bytes');
var client = dgram.createSocket('udp4');
const message = new Buffer('Some bytes');
const client = dgram.createSocket('udp4');
client.send(message, 0, message.length, 41234, 'localhost', (err) => {
client.close();
});
Expand Down
16 changes: 10 additions & 6 deletions doc/api/domain.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For example, this is not a good idea:
// XXX WARNING! BAD IDEA!

var d = require('domain').create();
d.on('error', function(er) {
d.on('error', (er) => {
// The error won't crash the process, but what it does is worse!
// Though we've prevented abrupt process restarting, we are leaking
// resources like crazy if this ever happens.
Expand Down Expand Up @@ -103,7 +103,7 @@ if (cluster.isMaster) {
// See the cluster documentation for more details about using
// worker processes to serve requests. How it works, caveats, etc.

var server = require('http').createServer((req, res) => {
const server = require('http').createServer((req, res) => {
var d = domain.create();
d.on('error', (er) => {
console.error('error', er.stack);
Expand Down Expand Up @@ -229,7 +229,9 @@ For example:

```
// create a top-level domain for the server
var serverDomain = domain.create();
const domain = require('domain');
const http = require('http');
const serverDomain = domain.create();

serverDomain.run(() => {
// server is created in the scope of serverDomain
Expand Down Expand Up @@ -281,7 +283,9 @@ This is the most basic way to use a domain.
Example:

```
var d = domain.create();
const domain = require('domain');
const fs = require('fs');
const d = domain.create();
d.on('error', (er) => {
console.error('Caught error!', er);
});
Expand Down Expand Up @@ -341,7 +345,7 @@ thrown will be routed to the domain's `'error'` event.

#### Example

var d = domain.create();
const d = domain.create();

function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.bind(function(er, data) {
Expand Down Expand Up @@ -370,7 +374,7 @@ with a single error handler in a single place.

#### Example

var d = domain.create();
const d = domain.create();

function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.intercept(function(data) {
Expand Down
4 changes: 2 additions & 2 deletions doc/api/errors.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ employed appropriately or [`process.on('uncaughtException')`][] has a handler.
```javascript
const net = require('net');

var connection = net.connect('localhost');
const connection = net.connect('localhost');

// adding an 'error' event handler to a stream:
connection.on('error', (err) => {
Expand All @@ -69,7 +69,7 @@ errors when no error handlers are attached. An example:
```javascript
const EventEmitter = require('events');

var ee = new EventEmitter();
const ee = new EventEmitter();

setImmediate(() => {
// this will crash the process because no 'error' event
Expand Down
Loading