Skip to content

Instantly share code, notes, and snippets.

@giangdhwhtbr
Forked from LeCoupa/nodejs-cheatsheet.js
Created October 13, 2016 01:58

Revisions

  1. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -518,4 +518,5 @@ buf.slice([start], [end]); // Returns a
    buf.fill(value, [offset], [end]); // Fills the buffer with the specified value
    buf[index]; // Get and set the octet at index
    buf.length; // The size of the buffer in bytes, Note that this is not necessarily the size of the contents

    buffer.INSPECT_MAX_BYTES; // How many bytes will be returned when buffer.inspect() is called. This can be overridden by user modules.
  2. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -213,7 +213,6 @@ EventEmitter.listenerCount(emitter, event); // Return the number of listeners f
    // Examples of readable streams include: http responses on the client, http requests on the server, fs read streams
    // zlib streams, crypto streams, tcp sockets, child process stdout and stderr, process.stdin.


    var readable = getReadableStreamSomehow();

    readable.on('readable', function() {}); // When a chunk of data can be read from the stream, it will emit a 'readable' event.
  3. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -215,6 +215,7 @@ EventEmitter.listenerCount(emitter, event); // Return the number of listeners f


    var readable = getReadableStreamSomehow();

    readable.on('readable', function() {}); // When a chunk of data can be read from the stream, it will emit a 'readable' event.
    readable.on('data', function(chunk) {}); // If you attach a data event listener, then it will switch the stream into flowing mode, and data will be passed to your handler as soon as it is available.
    readable.on('end', function() {}); // This event fires when there will be no more data to read.
    @@ -238,6 +239,7 @@ readable.unshift(chunk); // This is useful in certain cases whe
    // zlib streams, crypto streams, tcp sockets, child process stdin, process.stdout, process.stderr.

    var writer = getWritableStreamSomehow();

    writable.write(chunk, [encoding], [callback]); // This method writes some data to the underlying system, and calls the supplied callback once the data has been fully handled.
    writer.once('drain', write); // If a writable.write(chunk) call returns false, then the drain event will indicate when it is appropriate to begin writing more data to the stream.

  4. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -141,7 +141,8 @@ process.hrtime(); // Returns the current high-resolution rea


    // 6. Child Process.
    // Node provides a tri-directional popen(3) facility through the child_process module.
    // Node provides a tri-directional popen facility through the child_process module.
    // It is possible to stream data through a child's stdin, stdout, and stderr in a fully non-blocking way.
    // http://nodejs.org/api/child_process.html


  5. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -145,7 +145,7 @@ process.hrtime(); // Returns the current high-resolution rea
    // http://nodejs.org/api/child_process.html


    ChildProcess; // Class. ChildProcess is an EventEmitter.
    ChildProcess; // Class. ChildProcess is an EventEmitter.

    child.stdin; // A Writable Stream that represents the child process's stdin
    child.stdout; // A Readable Stream that represents the child process's stdout
  6. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -147,18 +147,18 @@ process.hrtime(); // Returns the current high-resolution rea

    ChildProcess; // Class. ChildProcess is an EventEmitter.

    child.stdin // A Writable Stream that represents the child process's stdin
    child.stdout // A Readable Stream that represents the child process's stdout
    child.stderr // A Readable Stream that represents the child process's stderr.
    child.pid // The PID of the child process
    child.connected // If .connected is false, it is no longer possible to send messages
    child.kill([signal]) // Send a signal to the child process
    child.send(message, [sendHandle]) // When using child_process.fork() you can write to the child using child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.
    child.disconnect() // Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.
    child_process.spawn(command, [args], [options]) // Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.
    child_process.exec(command, [options], callback) // Runs a command in a shell and buffers the output.
    child_process.execFile(file, [args], [options], [callback]) // Runs a command in a shell and buffers the output.
    child_process.fork(modulePath, [args], [options]) // This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.
    child.stdin; // A Writable Stream that represents the child process's stdin
    child.stdout; // A Readable Stream that represents the child process's stdout
    child.stderr; // A Readable Stream that represents the child process's stderr.
    child.pid; // The PID of the child process
    child.connected; // If .connected is false, it is no longer possible to send messages
    child.kill([signal]); // Send a signal to the child process
    child.send(message, [sendHandle]); // When using child_process.fork() you can write to the child using child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.
    child.disconnect(); // Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.
    child_process.spawn(command, [args], [options]); // Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.
    child_process.exec(command, [options], callback); // Runs a command in a shell and buffers the output.
    child_process.execFile(file, [args], [options], [callback]); // Runs a command in a shell and buffers the output.
    child_process.fork(modulePath, [args], [options]); // This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.


    // 7. Util.
  7. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 32 additions and 33 deletions.
    65 changes: 32 additions & 33 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -140,7 +140,28 @@ process.uptime(); // Number of seconds Node has been running
    process.hrtime(); // Returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array.


    // 6. Util.
    // 6. Child Process.
    // Node provides a tri-directional popen(3) facility through the child_process module.
    // http://nodejs.org/api/child_process.html


    ChildProcess; // Class. ChildProcess is an EventEmitter.

    child.stdin // A Writable Stream that represents the child process's stdin
    child.stdout // A Readable Stream that represents the child process's stdout
    child.stderr // A Readable Stream that represents the child process's stderr.
    child.pid // The PID of the child process
    child.connected // If .connected is false, it is no longer possible to send messages
    child.kill([signal]) // Send a signal to the child process
    child.send(message, [sendHandle]) // When using child_process.fork() you can write to the child using child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.
    child.disconnect() // Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.
    child_process.spawn(command, [args], [options]) // Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.
    child_process.exec(command, [options], callback) // Runs a command in a shell and buffers the output.
    child_process.execFile(file, [args], [options], [callback]) // Runs a command in a shell and buffers the output.
    child_process.fork(modulePath, [args], [options]) // This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.


    // 7. Util.
    // These functions are in the module 'util'. Use require('util') to access them.
    // http://nodejs.org/api/util.html

    @@ -160,7 +181,7 @@ util.isError(object); // Returns true if the given "object" is an Error
    util.inherits(constructor, superConstructor); // Inherit the prototype methods from one constructor into another.


    // 7. Events.
    // 8. Events.
    // All objects which emit events are instances of events.EventEmitter. You can access this module by doing: require("events");
    // To access the EventEmitter class, require('events').EventEmitter.
    // All EventEmitters emit the event 'newListener' when new listeners are added and 'removeListener' when a listener is removed.
    @@ -179,7 +200,7 @@ emitter.emit(event, [arg1], [arg2], [...]); // Execute each of the listeners in
    EventEmitter.listenerCount(emitter, event); // Return the number of listeners for a given event.


    // 8. Stream.
    // 9. Stream.
    // A stream is an abstract interface implemented by various objects in Node. For example a request to an HTTP server is a stream, as is stdout.
    // Streams are readable, writable, or both. All streams are instances of EventEmitter.
    // http://nodejs.org/api/stream.html
    @@ -233,7 +254,7 @@ writer.on('error', function(src) {}); // Emitted if there was an error
    // Examples of Transform streams include: zlib streams, crypto streams.


    // 9. File System.
    // 10. File System.
    // To use this module do require('fs').
    // All the methods have asynchronous and synchronous forms.
    // http://nodejs.org/api/fs.html
    @@ -325,7 +346,7 @@ fs.createReadStream(path, [options]); // Returns a new ReadStream object.
    fs.createWriteStream(path, [options]); // Returns a new WriteStream object.


    // 10. Path.
    // 11. Path.
    // Use require('path') to use this module.
    // This module contains utilities for handling and transforming file paths.
    // Almost all these methods perform only string transformations.
    @@ -345,7 +366,7 @@ path.sep; // The platform-specific file separator. '
    path.delimiter; // The platform-specific path delimiter, ';' or ':'.


    // 11. HTTP.
    // 12. HTTP.
    // To use the HTTP server and client one must require('http').
    // http://nodejs.org/api/http.html

    @@ -413,7 +434,7 @@ message.socket; // The net.Socket object associated with
    message.setTimeout(msecs, callback); // Calls message.connection.setTimeout(msecs, callback).


    // 12. URL.
    // 13. URL.
    // This module has utilities for URL resolution and parsing. Call require('url') to use it.
    // http://nodejs.org/api/url.html

    @@ -423,7 +444,7 @@ url.format(urlObj); // Take a parsed UR
    url.resolve(from, to); // Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.


    // 13. Query String.
    // 14. Query String.
    // This module provides utilities for dealing with query strings. Call require('querystring') to use it.
    // http://nodejs.org/api/querystring.html

    @@ -432,7 +453,7 @@ querystring.stringify(obj, [sep], [eq]); // Serialize an object to a que
    querystring.parse(str, [sep], [eq], [options]); // Deserialize a query string to an object. Optionally override the default separator ('&') and assignment ('=') characters.


    // 14. Assert.
    // 15. Assert.
    // This module is used for writing unit tests for your applications, you can access it with require('assert').
    // http://nodejs.org/api/assert.html

    @@ -450,7 +471,7 @@ assert.doesNotThrow(block, [message]); // Expects block not to th
    assert.ifError(value); // Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.


    // 15. OS.
    // 16. OS.
    // Provides a few basic operating-system related utility functions.
    // Use require('os') to access this module.
    // http://nodejs.org/api/os.html
    @@ -472,7 +493,7 @@ os.networkInterfaces(); // Get a list of network interfaces.
    os.EOL; // A constant defining the appropriate End-of-line marker for the operating system.


    // 16. Buffer.
    // 17. Buffer.
    // Buffer is used to dealing with binary data
    // Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap
    // http://nodejs.org/api/buffer.html
    @@ -496,25 +517,3 @@ buf.fill(value, [offset], [end]); // Fills the
    buf[index]; // Get and set the octet at index
    buf.length; // The size of the buffer in bytes, Note that this is not necessarily the size of the contents
    buffer.INSPECT_MAX_BYTES; // How many bytes will be returned when buffer.inspect() is called. This can be overridden by user modules.



    // 17. child_process.
    // Node provides a tri-directional popen(3) facility through the child_process module.
    // http://nodejs.org/api/child_process.html


    ChildProcess; // Class. ChildProcess is an EventEmitter.

    child.stdin // A Writable Stream that represents the child process's stdin
    child.stdout // A Readable Stream that represents the child process's stdout
    child.stderr // A Readable Stream that represents the child process's stderr.
    child.pid // The PID of the child process
    child.connected // If .connected is false, it is no longer possible to send messages
    child.kill([signal]) // Send a signal to the child process
    child.send(message, [sendHandle]) // When using child_process.fork() you can write to the child using child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.
    child.disconnect() // Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.
    child_process.spawn(command, [args], [options]) // Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.
    child_process.exec(command, [options], callback) // Runs a command in a shell and buffers the output.
    child_process.execFile(file, [args], [options], [callback]) // Runs a command in a shell and buffers the output.
    child_process.fork(modulePath, [args], [options]) // This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.
  8. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -504,17 +504,17 @@ buffer.INSPECT_MAX_BYTES; // How many
    // http://nodejs.org/api/child_process.html


    ChildProcess; // Class. ChildProcess is an EventEmitter.

    child.stdin // A Writable Stream that represents the child process's stdin
    child.stdout // A Readable Stream that represents the child process's stdout
    child.stderr // A Readable Stream that represents the child process's stderr.
    child.pid // The PID of the child process
    child.connected // If .connected is false, it is no longer possible to send messages
    child.kill([signal]) // Send a signal to the child process
    child.send(message, [sendHandle]) // When using child_process.fork() you can write to the child using child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.
    child.disconnect() // Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.
    ChildProcess; // Class. ChildProcess is an EventEmitter.

    child.stdin // A Writable Stream that represents the child process's stdin
    child.stdout // A Readable Stream that represents the child process's stdout
    child.stderr // A Readable Stream that represents the child process's stderr.
    child.pid // The PID of the child process
    child.connected // If .connected is false, it is no longer possible to send messages
    child.kill([signal]) // Send a signal to the child process
    child.send(message, [sendHandle]) // When using child_process.fork() you can write to the child using child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.
    child.disconnect() // Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.
    child_process.spawn(command, [args], [options]) // Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.
    child_process.exec(command, [options], callback) // Runs a command in a shell and buffers the output.
    child_process.exec(command, [options], callback) // Runs a command in a shell and buffers the output.
    child_process.execFile(file, [args], [options], [callback]) // Runs a command in a shell and buffers the output.
    child_process.fork(modulePath, [args], [options]) // This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.
  9. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -478,24 +478,24 @@ os.EOL; // A constant defining the appropriate End-of-line mark
    // http://nodejs.org/api/buffer.html


    new Buffer(size); // Allocates a new buffer of size octets.
    new Buffer(array); // Allocates a new buffer using an array of octets.
    new Buffer(str, [encoding]); // Allocates a new buffer containing the given str. encoding defaults to 'utf8'.
    new Buffer(size); // Allocates a new buffer of size octets.
    new Buffer(array); // Allocates a new buffer using an array of octets.
    new Buffer(str, [encoding]); // Allocates a new buffer containing the given str. encoding defaults to 'utf8'.

    Buffer.isEncoding(encoding); // Returns true if the encoding is a valid encoding argument, or false otherwise.
    Buffer.isBuffer(obj); // Tests if obj is a Buffer
    Buffer.concat(list, [totalLength]); // Returns a buffer which is the result of concatenating all the buffers in the list together.
    Buffer.byteLength(string, [encoding]); // Gives the actual byte length of a string.
    Buffer.isEncoding(encoding); // Returns true if the encoding is a valid encoding argument, or false otherwise.
    Buffer.isBuffer(obj); // Tests if obj is a Buffer
    Buffer.concat(list, [totalLength]); // Returns a buffer which is the result of concatenating all the buffers in the list together.
    Buffer.byteLength(string, [encoding]); // Gives the actual byte length of a string.

    buf.write(string, [offset], [length], [encoding]); // Writes string to the buffer at offset using the given encoding
    buf.toString([encoding], [start], [end]); // Decodes and returns a string from buffer data encoded with encoding (defaults to 'utf8') beginning at start (defaults to 0) and ending at end (defaults to buffer.length).
    buf.write(string, [offset], [length], [encoding]); // Writes string to the buffer at offset using the given encoding
    buf.toString([encoding], [start], [end]); // Decodes and returns a string from buffer data encoded with encoding (defaults to 'utf8') beginning at start (defaults to 0) and ending at end (defaults to buffer.length).
    buf.toJSON(); // Returns a JSON-representation of the Buffer instance, which is identical to the output for JSON Arrays
    buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd]); // Does copy between buffers. The source and target regions can be overlapped
    buf.slice([start], [end]); // Returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.
    buf.fill(value, [offset], [end]); // Fills the buffer with the specified value
    buf[index]; // Get and set the octet at index
    buf.length; // The size of the buffer in bytes, Note that this is not necessarily the size of the contents
    buffer.INSPECT_MAX_BYTES; // How many bytes will be returned when buffer.inspect() is called. This can be overridden by user modules.
    buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd]); // Does copy between buffers. The source and target regions can be overlapped
    buf.slice([start], [end]); // Returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.
    buf.fill(value, [offset], [end]); // Fills the buffer with the specified value
    buf[index]; // Get and set the octet at index
    buf.length; // The size of the buffer in bytes, Note that this is not necessarily the size of the contents
    buffer.INSPECT_MAX_BYTES; // How many bytes will be returned when buffer.inspect() is called. This can be overridden by user modules.



  10. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 48 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -470,3 +470,51 @@ os.freemem(); // Returns the amount of free system memory in bytes.
    os.cpus(); // Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).
    os.networkInterfaces(); // Get a list of network interfaces.
    os.EOL; // A constant defining the appropriate End-of-line marker for the operating system.


    // 16. Buffer.
    // Buffer is used to dealing with binary data
    // Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap
    // http://nodejs.org/api/buffer.html


    new Buffer(size); // Allocates a new buffer of size octets.
    new Buffer(array); // Allocates a new buffer using an array of octets.
    new Buffer(str, [encoding]); // Allocates a new buffer containing the given str. encoding defaults to 'utf8'.

    Buffer.isEncoding(encoding); // Returns true if the encoding is a valid encoding argument, or false otherwise.
    Buffer.isBuffer(obj); // Tests if obj is a Buffer
    Buffer.concat(list, [totalLength]); // Returns a buffer which is the result of concatenating all the buffers in the list together.
    Buffer.byteLength(string, [encoding]); // Gives the actual byte length of a string.

    buf.write(string, [offset], [length], [encoding]); // Writes string to the buffer at offset using the given encoding
    buf.toString([encoding], [start], [end]); // Decodes and returns a string from buffer data encoded with encoding (defaults to 'utf8') beginning at start (defaults to 0) and ending at end (defaults to buffer.length).
    buf.toJSON(); // Returns a JSON-representation of the Buffer instance, which is identical to the output for JSON Arrays
    buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd]); // Does copy between buffers. The source and target regions can be overlapped
    buf.slice([start], [end]); // Returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.
    buf.fill(value, [offset], [end]); // Fills the buffer with the specified value
    buf[index]; // Get and set the octet at index
    buf.length; // The size of the buffer in bytes, Note that this is not necessarily the size of the contents
    buffer.INSPECT_MAX_BYTES; // How many bytes will be returned when buffer.inspect() is called. This can be overridden by user modules.



    // 17. child_process.
    // Node provides a tri-directional popen(3) facility through the child_process module.
    // http://nodejs.org/api/child_process.html


    ChildProcess; // Class. ChildProcess is an EventEmitter.

    child.stdin // A Writable Stream that represents the child process's stdin
    child.stdout // A Readable Stream that represents the child process's stdout
    child.stderr // A Readable Stream that represents the child process's stderr.
    child.pid // The PID of the child process
    child.connected // If .connected is false, it is no longer possible to send messages
    child.kill([signal]) // Send a signal to the child process
    child.send(message, [sendHandle]) // When using child_process.fork() you can write to the child using child.send(message, [sendHandle]) and messages are received by a 'message' event on the child.
    child.disconnect() // Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.
    child_process.spawn(command, [args], [options]) // Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.
    child_process.exec(command, [options], callback) // Runs a command in a shell and buffers the output.
    child_process.execFile(file, [args], [options], [callback]) // Runs a command in a shell and buffers the output.
    child_process.fork(modulePath, [args], [options]) // This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.
  11. Julien Le Coupanec revised this gist Jun 24, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@ __dirname; // The name of the directory that the currently executing script re
    module; // A reference to the current module. In particular module.exports is used for defining what a module exports and makes available through require().
    exports; // A reference to the module.exports that is shorter to type.
    process; // The process object is a global object and can be accessed from anywhere. It is an instance of EventEmitter.
    Buffer; // The Buffer class is a global type for dealing with binary data directly.


    // 2. Console.
  12. Julien Le Coupanec renamed this gist Jun 16, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. Julien Le Coupanec revised this gist Jun 16, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -436,7 +436,7 @@ querystring.parse(str, [sep], [eq], [options]); // Deserialize a query string t
    // http://nodejs.org/api/assert.html


    assert.fail(actual, expected, message, operator); // Throws an exception that displays the values for actual and expected separated by the provided operator.
    assert.fail(actual, expected, message, operator); // Throws an exception that displays the values for actual and expected separated by the provided operator.
    assert(value, message); assert.ok(value, [message]); // Tests if value is truthy, it is equivalent to assert.equal(true, !!value, message);
    assert.equal(actual, expected, [message]); // Tests shallow, coercive equality with the equal comparison operator ( == ).
    assert.notEqual(actual, expected, [message]); // Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
  14. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -455,16 +455,17 @@ assert.ifError(value); // Tests if value is not a
    // http://nodejs.org/api/os.html


    os.tmpdir(); // Returns the operating system's default directory for temp files.
    os.endianness(); // Returns the endianness of the CPU. Possible values are "BE" or "LE".
    os.hostname(); // Returns the hostname of the operating system.
    os.type(); // Returns the operating system name.
    os.platform(); // Returns the operating system platform.
    os.arch(); // Returns the operating system CPU architecture.
    os.release(); // Returns the operating system release.






    os.tmpdir(); // Returns the operating system's default directory for temp files.
    os.endianness(); // Returns the endianness of the CPU. Possible values are "BE" or "LE".
    os.hostname(); // Returns the hostname of the operating system.
    os.type(); // Returns the operating system name.
    os.platform(); // Returns the operating system platform.
    os.arch(); // Returns the operating system CPU architecture.
    os.release(); // Returns the operating system release.
    os.uptime(); // Returns the system uptime in seconds.
    os.loadavg(); // Returns an array containing the 1, 5, and 15 minute load averages.
    os.totalmem(); // Returns the total amount of system memory in bytes.
    os.freemem(); // Returns the amount of free system memory in bytes.
    os.cpus(); // Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).
    os.networkInterfaces(); // Get a list of network interfaces.
    os.EOL; // A constant defining the appropriate End-of-line marker for the operating system.
  15. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -456,4 +456,15 @@ assert.ifError(value); // Tests if value is not a


    os.tmpdir(); // Returns the operating system's default directory for temp files.
    os.endianness(); // os.endianness()
    os.endianness(); // Returns the endianness of the CPU. Possible values are "BE" or "LE".
    os.hostname(); // Returns the hostname of the operating system.
    os.type(); // Returns the operating system name.
    os.platform(); // Returns the operating system platform.
    os.arch(); // Returns the operating system CPU architecture.
    os.release(); // Returns the operating system release.






  16. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -449,3 +449,11 @@ assert.doesNotThrow(block, [message]); // Expects block not to th
    assert.ifError(value); // Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.


    // 15. OS.
    // Provides a few basic operating-system related utility functions.
    // Use require('os') to access this module.
    // http://nodejs.org/api/os.html


    os.tmpdir(); // Returns the operating system's default directory for temp files.
    os.endianness(); // os.endianness()
  17. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -431,4 +431,21 @@ querystring.stringify(obj, [sep], [eq]); // Serialize an object to a que
    querystring.parse(str, [sep], [eq], [options]); // Deserialize a query string to an object. Optionally override the default separator ('&') and assignment ('=') characters.


    // 14. Assert.
    // This module is used for writing unit tests for your applications, you can access it with require('assert').
    // http://nodejs.org/api/assert.html


    assert.fail(actual, expected, message, operator); // Throws an exception that displays the values for actual and expected separated by the provided operator.
    assert(value, message); assert.ok(value, [message]); // Tests if value is truthy, it is equivalent to assert.equal(true, !!value, message);
    assert.equal(actual, expected, [message]); // Tests shallow, coercive equality with the equal comparison operator ( == ).
    assert.notEqual(actual, expected, [message]); // Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
    assert.deepEqual(actual, expected, [message]); // Tests for deep equality.
    assert.notDeepEqual(actual, expected, [message]); // Tests for any deep inequality.
    assert.strictEqual(actual, expected, [message]); // Tests strict equality, as determined by the strict equality operator ( === )
    assert.notStrictEqual(actual, expected, [message]); // Tests strict non-equality, as determined by the strict not equal operator ( !== )
    assert.throws(block, [error], [message]); // Expects block to throw an error. error can be constructor, RegExp or validation function.
    assert.doesNotThrow(block, [message]); // Expects block not to throw an error, see assert.throws for details.
    assert.ifError(value); // Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.


  18. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -431,7 +431,4 @@ querystring.stringify(obj, [sep], [eq]); // Serialize an object to a que
    querystring.parse(str, [sep], [eq], [options]); // Deserialize a query string to an object. Optionally override the default separator ('&') and assignment ('=') characters.


    // 14. REPL.
    // By executing node without any arguments from the command-line you will be dropped into the REPL.
    // http://nodejs.org/api/repl.html

  19. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -419,4 +419,19 @@ message.setTimeout(msecs, callback); // Calls message.connection.setTimeout(m

    url.parse(urlStr, [parseQueryString], [slashesDenoteHost]); // Take a URL string, and return an object.
    url.format(urlObj); // Take a parsed URL object, and return a formatted URL string.
    url.resolve(from, to); // Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.
    url.resolve(from, to); // Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.


    // 13. Query String.
    // This module provides utilities for dealing with query strings. Call require('querystring') to use it.
    // http://nodejs.org/api/querystring.html


    querystring.stringify(obj, [sep], [eq]); // Serialize an object to a query string. Optionally override the default separator ('&') and assignment ('=') characters.
    querystring.parse(str, [sep], [eq], [options]); // Deserialize a query string to an object. Optionally override the default separator ('&') and assignment ('=') characters.


    // 14. REPL.
    // By executing node without any arguments from the command-line you will be dropped into the REPL.
    // http://nodejs.org/api/repl.html

  20. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -418,5 +418,5 @@ message.setTimeout(msecs, callback); // Calls message.connection.setTimeout(m


    url.parse(urlStr, [parseQueryString], [slashesDenoteHost]); // Take a URL string, and return an object.
    url.format(urlObj); // Take a parsed URL object, and return a formatted URL string.
    url.resolve(from, to); // Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.
    url.format(urlObj); // Take a parsed URL object, and return a formatted URL string.
    url.resolve(from, to); // Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.
  21. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -324,7 +324,7 @@ fs.createReadStream(path, [options]); // Returns a new ReadStream object.
    fs.createWriteStream(path, [options]); // Returns a new WriteStream object.


    // 10. File System.
    // 10. Path.
    // Use require('path') to use this module.
    // This module contains utilities for handling and transforming file paths.
    // Almost all these methods perform only string transformations.
    @@ -410,3 +410,13 @@ message.statusCode; // The 3-digit HTTP response status code
    message.socket; // The net.Socket object associated with the connection.

    message.setTimeout(msecs, callback); // Calls message.connection.setTimeout(msecs, callback).


    // 12. URL.
    // This module has utilities for URL resolution and parsing. Call require('url') to use it.
    // http://nodejs.org/api/url.html


    url.parse(urlStr, [parseQueryString], [slashesDenoteHost]); // Take a URL string, and return an object.
    url.format(urlObj); // Take a parsed URL object, and return a formatted URL string.
    url.resolve(from, to); // Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.
  22. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -400,3 +400,13 @@ response.sendDate; // When true, the

    response.on('close', function () { }); // Indicates that the underlying connection was terminated before response.end() was called or able to flush.
    response.on('finish', function() { }); // Emitted when the response has been sent.

    message.httpVersion; // In case of server request, the HTTP version sent by the client. In the case of client response, the HTTP version of the connected-to server.
    message.headers; // The request/response headers object.
    message.trailers; // The request/response trailers object. Only populated after the 'end' event.
    message.method; // The request method as a string. Read only. Example: 'GET', 'DELETE'.
    message.url; // Request URL string. This contains only the URL that is present in the actual HTTP request.
    message.statusCode; // The 3-digit HTTP response status code. E.G. 404.
    message.socket; // The net.Socket object associated with the connection.

    message.setTimeout(msecs, callback); // Calls message.connection.setTimeout(msecs, callback).
  23. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -349,16 +349,16 @@ path.delimiter; // The platform-specific path delimiter, '
    // http://nodejs.org/api/http.html


    http.STATUS_CODES; // A collection of all the standard HTTP response status codes, and the short description of each.
    http.STATUS_CODES; // A collection of all the standard HTTP response status codes, and the short description of each.
    http.request(options, [callback]); // This function allows one to transparently issue requests.
    http.get(options, [callback]); // Set the method to GET and calls req.end() automatically.

    server = http.createServer([requestListener]); // Returns a new web server object. The requestListener is a function which is automatically added to the 'request' event.
    server.listen(port, [hostname], [backlog], [callback]); // Begin accepting connections on the specified port and hostname.
    server.listen(path, [callback]); // Start a UNIX socket server listening for connections on the given path.
    server.listen(handle, [callback]); // The handle object can be set to either a server or socket (anything with an underlying _handle member), or a {fd: <n>} object.
    server.close([callback]); // Stops the server from accepting new connections.
    server.setTimeout(msecs, callback); // Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as an argument, if a timeout occurs.
    server = http.createServer([requestListener]); // Returns a new web server object. The requestListener is a function which is automatically added to the 'request' event.
    server.listen(port, [hostname], [backlog], [callback]); // Begin accepting connections on the specified port and hostname.
    server.listen(path, [callback]); // Start a UNIX socket server listening for connections on the given path.
    server.listen(handle, [callback]); // The handle object can be set to either a server or socket (anything with an underlying _handle member), or a {fd: <n>} object.
    server.close([callback]); // Stops the server from accepting new connections.
    server.setTimeout(msecs, callback); // Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as an argument, if a timeout occurs.

    server.maxHeadersCount; // Limits maximum incoming headers count, equal to 1000 by default. If set to 0 - no limit will be applied.
    server.timeout; // The number of milliseconds of inactivity before a socket is presumed to have timed out.
  24. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -371,9 +371,18 @@ server.on('connect', function (request, socket, head) { }); // Emitted each t
    server.on('upgrade', function (request, socket, head) { }); // Emitted each time a client requests a http upgrade.
    server.on('clientError', function (exception, socket) { }); // If a client connection emits an 'error' event - it will forwarded here.

    request.write(chunk, [encoding]); // Sends a chunk of the body.
    request.end([data], [encoding]); // Finishes sending the request. If any parts of the body are unsent, it will flush them to the stream.
    request.abort(); // Aborts a request.
    request.setTimeout(timeout, [callback]); // Once a socket is assigned to this request and is connected socket.setTimeout() will be called.
    request.setNoDelay([noDelay]); // Once a socket is assigned to this request and is connected socket.setNoDelay() will be called.
    request.setSocketKeepAlive([enable], [initialDelay]); // Once a socket is assigned to this request and is connected socket.setKeepAlive() will be called.

    request.on('response', function(response) { }); // Emitted when a response is received to this request. This event is emitted only once.
    request.on('socket', function(socket) { }); // Emitted after a socket is assigned to this request.
    request.on('connect', function(response, socket, head) { }); // Emitted each time a server responds to a request with a CONNECT method. If this event isn't being listened for, clients receiving a CONNECT method will have their connections closed.
    request.on('upgrade', function(response, socket, head) { }); // Emitted each time a server responds to a request with an upgrade. If this event isn't being listened for, clients receiving an upgrade header will have their connections closed.
    request.on('continue', function() { }); // Emitted when the server sends a '100 Continue' HTTP response, usually because the request contained 'Expect: 100-continue'. This is an instruction that the client should send the request body.

    response.write(chunk, [encoding]); // This sends a chunk of the response body. If this merthod is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
    response.writeContinue(); // Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
  25. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 13 additions and 10 deletions.
    23 changes: 13 additions & 10 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -349,14 +349,16 @@ path.delimiter; // The platform-specific path delimiter, '
    // http://nodejs.org/api/http.html


    http.STATUS_CODES; // A collection of all the standard HTTP response status codes, and the short description of each.
    http.STATUS_CODES; // A collection of all the standard HTTP response status codes, and the short description of each.
    http.request(options, [callback]); // This function allows one to transparently issue requests.
    http.get(options, [callback]); // Set the method to GET and calls req.end() automatically.

    server = http.createServer([requestListener]); // Returns a new web server object. The requestListener is a function which is automatically added to the 'request' event.
    server.listen(port, [hostname], [backlog], [callback]); // Begin accepting connections on the specified port and hostname.
    server.listen(path, [callback]); // Start a UNIX socket server listening for connections on the given path.
    server.listen(handle, [callback]); // The handle object can be set to either a server or socket (anything with an underlying _handle member), or a {fd: <n>} object.
    server.close([callback]); // Stops the server from accepting new connections.
    server.setTimeout(msecs, callback); // Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as an argument, if a timeout occurs.
    server = http.createServer([requestListener]); // Returns a new web server object. The requestListener is a function which is automatically added to the 'request' event.
    server.listen(port, [hostname], [backlog], [callback]); // Begin accepting connections on the specified port and hostname.
    server.listen(path, [callback]); // Start a UNIX socket server listening for connections on the given path.
    server.listen(handle, [callback]); // The handle object can be set to either a server or socket (anything with an underlying _handle member), or a {fd: <n>} object.
    server.close([callback]); // Stops the server from accepting new connections.
    server.setTimeout(msecs, callback); // Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as an argument, if a timeout occurs.

    server.maxHeadersCount; // Limits maximum incoming headers count, equal to 1000 by default. If set to 0 - no limit will be applied.
    server.timeout; // The number of milliseconds of inactivity before a socket is presumed to have timed out.
    @@ -369,10 +371,11 @@ server.on('connect', function (request, socket, head) { }); // Emitted each t
    server.on('upgrade', function (request, socket, head) { }); // Emitted each time a client requests a http upgrade.
    server.on('clientError', function (exception, socket) { }); // If a client connection emits an 'error' event - it will forwarded here.

    http.request(options, [callback]); // This function allows one to transparently issue requests.
    http.get(options, [callback]); // Set the method to GET and calls req.end() automatically.
    request.on('response', function(response) { }); // Emitted when a response is received to this request. This event is emitted only once.
    request.on('socket', function(socket) { }); // Emitted after a socket is assigned to this request.
    request.on('connect', function(response, socket, head) { }); // Emitted each time a server responds to a request with a CONNECT method. If this event isn't being listened for, clients receiving a CONNECT method will have their connections closed.

    response.write(chunk, [encoding]); // This sends a chunk of the response body. If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
    response.write(chunk, [encoding]); // This sends a chunk of the response body. If this merthod is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
    response.writeContinue(); // Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
    response.writeHead(statusCode, [reasonPhrase], [headers]); // Sends a response header to the request.
    response.setTimeout(msecs, callback); // Sets the Socket's timeout value to msecs. If a callback is provided, then it is added as a listener on the 'timeout' event on the response object.
  26. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -370,7 +370,7 @@ server.on('upgrade', function (request, socket, head) { }); // Emitted each t
    server.on('clientError', function (exception, socket) { }); // If a client connection emits an 'error' event - it will forwarded here.

    http.request(options, [callback]); // This function allows one to transparently issue requests.
    http.get(options, [callback]); // Set the method to GET and calls req.end() automatically.
    http.get(options, [callback]); // Set the method to GET and calls req.end() automatically.

    response.write(chunk, [encoding]); // This sends a chunk of the response body. If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
    response.writeContinue(); // Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
    @@ -380,7 +380,7 @@ response.setHeader(name, value); // Sets a single
    response.getHeader(name); // Reads out a header that's already been queued but not sent to the client. Note that the name is case insensitive.
    response.removeHeader(name); // Removes a header that's queued for implicit sending.
    response.addTrailers(headers); // This method adds HTTP trailing headers (a header but at the end of the message) to the response.
    response.end([data], [encoding]); // This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.
    response.end([data], [encoding]); // This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

    response.statusCode; // When using implicit headers (not calling response.writeHead() explicitly), this property controls the status code that will be sent to the client when the headers get flushed.
    response.headersSent; // Boolean (read-only). True if headers were sent, false otherwise.
  27. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -369,13 +369,18 @@ server.on('connect', function (request, socket, head) { }); // Emitted each t
    server.on('upgrade', function (request, socket, head) { }); // Emitted each time a client requests a http upgrade.
    server.on('clientError', function (exception, socket) { }); // If a client connection emits an 'error' event - it will forwarded here.

    response.write(chunk, [encoding]); // If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
    http.request(options, [callback]); // This function allows one to transparently issue requests.
    http.get(options, [callback]); // Set the method to GET and calls req.end() automatically.

    response.write(chunk, [encoding]); // This sends a chunk of the response body. If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
    response.writeContinue(); // Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
    response.writeHead(statusCode, [reasonPhrase], [headers]); // Sends a response header to the request.
    response.setTimeout(msecs, callback); // Sets the Socket's timeout value to msecs. If a callback is provided, then it is added as a listener on the 'timeout' event on the response object.
    response.setHeader(name, value); // Sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings here if you need to send multiple headers with the same name.
    response.getHeader(name); // Reads out a header that's already been queued but not sent to the client. Note that the name is case insensitive.
    response.removeHeader(name); // Removes a header that's queued for implicit sending.
    response.addTrailers(headers); // This method adds HTTP trailing headers (a header but at the end of the message) to the response.
    response.end([data], [encoding]); // This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

    response.statusCode; // When using implicit headers (not calling response.writeHead() explicitly), this property controls the status code that will be sent to the client when the headers get flushed.
    response.headersSent; // Boolean (read-only). True if headers were sent, false otherwise.
  28. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -369,6 +369,7 @@ server.on('connect', function (request, socket, head) { }); // Emitted each t
    server.on('upgrade', function (request, socket, head) { }); // Emitted each time a client requests a http upgrade.
    server.on('clientError', function (exception, socket) { }); // If a client connection emits an 'error' event - it will forwarded here.

    response.write(chunk, [encoding]); // If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
    response.writeContinue(); // Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
    response.writeHead(statusCode, [reasonPhrase], [headers]); // Sends a response header to the request.
    response.setTimeout(msecs, callback); // Sets the Socket's timeout value to msecs. If a callback is provided, then it is added as a listener on the 'timeout' event on the response object.
  29. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -370,12 +370,15 @@ server.on('upgrade', function (request, socket, head) { }); // Emitted each t
    server.on('clientError', function (exception, socket) { }); // If a client connection emits an 'error' event - it will forwarded here.

    response.writeContinue(); // Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
    response.writeHead(statusCode, [reasonPhrase], [headers]); // Sends a response header to the request.
    response.writeHead(statusCode, [reasonPhrase], [headers]); // Sends a response header to the request.
    response.setTimeout(msecs, callback); // Sets the Socket's timeout value to msecs. If a callback is provided, then it is added as a listener on the 'timeout' event on the response object.
    response.setHeader(name, value); // Sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings here if you need to send multiple headers with the same name.
    response.getHeader(name); // Reads out a header that's already been queued but not sent to the client. Note that the name is case insensitive.
    response.removeHeader(name); // Removes a header that's queued for implicit sending.

    response.statusCode; // When using implicit headers (not calling response.writeHead() explicitly), this property controls the status code that will be sent to the client when the headers get flushed.

    response.headersSent; // Boolean (read-only). True if headers were sent, false otherwise.
    response.sendDate; // When true, the Date header will be automatically generated and sent in the response if it is not already present in the headers. Defaults to true.

    response.on('close', function () { }); // Indicates that the underlying connection was terminated before response.end() was called or able to flush.
    response.on('finish', function() { }); // Emitted when the response has been sent.
  30. Julien Le Coupanec revised this gist Jun 15, 2014. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion 1-nodejs-cheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -351,10 +351,15 @@ path.delimiter; // The platform-specific path delimiter, '

    http.STATUS_CODES; // A collection of all the standard HTTP response status codes, and the short description of each.

    http.createServer([requestListener]); // Returns a new web server object. The requestListener is a function which is automatically added to the 'request' event.
    server = http.createServer([requestListener]); // Returns a new web server object. The requestListener is a function which is automatically added to the 'request' event.
    server.listen(port, [hostname], [backlog], [callback]); // Begin accepting connections on the specified port and hostname.
    server.listen(path, [callback]); // Start a UNIX socket server listening for connections on the given path.
    server.listen(handle, [callback]); // The handle object can be set to either a server or socket (anything with an underlying _handle member), or a {fd: <n>} object.
    server.close([callback]); // Stops the server from accepting new connections.
    server.setTimeout(msecs, callback); // Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as an argument, if a timeout occurs.

    server.maxHeadersCount; // Limits maximum incoming headers count, equal to 1000 by default. If set to 0 - no limit will be applied.
    server.timeout; // The number of milliseconds of inactivity before a socket is presumed to have timed out.

    server.on('request', function (request, response) { }); // Emitted each time there is a request.
    server.on('connection', function (socket) { }); // When a new TCP stream is established.
    @@ -364,3 +369,13 @@ server.on('connect', function (request, socket, head) { }); // Emitted each t
    server.on('upgrade', function (request, socket, head) { }); // Emitted each time a client requests a http upgrade.
    server.on('clientError', function (exception, socket) { }); // If a client connection emits an 'error' event - it will forwarded here.

    response.writeContinue(); // Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
    response.writeHead(statusCode, [reasonPhrase], [headers]); // Sends a response header to the request.
    response.setTimeout(msecs, callback); // Sets the Socket's timeout value to msecs. If a callback is provided, then it is added as a listener on the 'timeout' event on the response object.
    response.setHeader(name, value); // Sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings here if you need to send multiple headers with the same name.

    response.statusCode; // When using implicit headers (not calling response.writeHead() explicitly), this property controls the status code that will be sent to the client when the headers get flushed.


    response.on('close', function () { }); // Indicates that the underlying connection was terminated before response.end() was called or able to flush.
    response.on('finish', function() { }); // Emitted when the response has been sent.