Camilo Herbert - Node Cheatsheet
Camilo Herbert - Node Cheatsheet
path.resolve([from ...], to); Emitted each time a client Sets a single header value for
Solve the relative path from 'from' requests a http CONNECT method. implicit headers. If this header
to 'to'. server.on('connect', function already exists in the to-be-sent
path.relative(from, to); (request, socket, head) { }); headers, its value will be
Return the directory name of a Emitted each time a client replaced. Use an array of strings
path. Similar to the Unix dirname requests a http upgrade. here if you need to send multiple
command. server.on('upgrade', function headers with the same name.
path.dirname(p); (request, socket, head) { }); response.setHeader(name, value);
Return the last portion of a path. If a client connection emits an Reads out a header that's already
Similar to the Unix basename 'error' event - it will forwarded been queued but not sent to the
command. here. client. Note that the name is case
path.basename(p, [ext]); server.on('clientError', function insensitive.
Return the extension of the path, (exception, socket) { }); response.getHeader(name);
from the last '.' to end of string Removes a header that's queued for
in the last portion of the path. HTTP - Responses implicit sending.
path.extname(p); response.removeHeader(name);
This sends a chunk of the response
The platform-specific file This method adds HTTP trailing
body. If this merthod is called and
separator. '\\' or '/'. headers (a header but at the end of
response.writeHead() has not been
path.sep; the message) to the response.
called, it will switch to implicit
The platform-specific path response.addTrailers(headers);
header mode and flush the implicit
delimiter, ';' or ':'. This method signals to the server
headers.
path.delimiter; that all of the response headers
response.write(chunk, [encoding]);
and body have been sent; that
Sends a HTTP/1.1 100 Continue
HTTP - Server Events server should consider this message
message to the client, indicating
complete. The method,
Emitted each time there is a that the request body should be
response.end(), MUST be called on
request. sent.
each response.
server.on('request', function response.writeContinue();
response.end([data], [encoding]);
(request, response) { }); Sends a response header to the
When using implicit headers (not
When a new TCP stream is request.
calling response.writeHead()
established. response.writeHead(statusCode,
explicitly), this property controls
server.on('connection', function [reasonPhrase], [headers]);
the status code that will be sent
(socket) { }); Sets the Socket's timeout value to
to the client when
Emitted when the server closes. msecs. If a callback is provided,
the headers get flushed.
server.on('close', function () { then it is added as a listener on
response.statusCode;
}); the 'timeout' event on the response
Boolean (read-only). True if
Emitted each time a request with an object.
headers were sent, false
http Expect: 100-continue is response.setTimeout(msecs,
otherwise.
received. callback);
response.headersSent;
server.on('checkContinue', function
(request, response) { });
When true, the Date header will be Same as assert.ok() where if the var readable =
automatically generated and sent in expression evaluates as false throw getReadableStreamSomehow();
the response if it is not already an AssertionError with message. When a chunk of data can be read
present in the headers. Defaults to console.assert(expression, from the stream, it will emit a
true. [message]); 'readable' event.
response.sendDate; readable.on('readable', function()
Indicates that the underlying Process {});
connection was terminated before If you attach a data event
response.end() was called or able listener, then it will switch the
Emitted when the process is about
to flush. stream into flowing mode, and data
to exit
response.on('close', function () { will be passed to your handler as
process.on('exit', function(code)
}); soon as it is available.
{});
Emitted when the response has been readable.on('data',
Emitted when an exception bubbles
sent. function(chunk) {});
all the way back to the event loop.
response.on('finish', function() { This event fires when there will be
(should not be used)
}); no more data to read.
process.on('uncaughtException',
readable.on('end', function() {});
function(err) {});
Console Emitted when the underlying
A writable stream to stdout.
resource (for example, the backing
Prints to stdout with newline. process.stdout;
file descriptor) has been closed.
console.log([data], [...]); A writable stream to stderr.
Not all streams will emit this.
Same as console.log. process.stderr;
readable.on('close', function()
console.info([data], [...]); A readable stream for stdin.
{});
Same as console.log but prints to process.stdin;
Emitted if there was an error
stderr. An array containing the command
receiving data.
console.error([data], [...]); line arguments.
readable.on('error', function()
Same as console.error. process.argv;
{});
console.warn([data], [...]); An object containing the user
The read() method pulls some data
Uses util.inspect on obj and prints environment.
out of the internal buffer and
resulting string to stdout. process.env;
returns it. If there is no data
console.dir(obj); This is the absolute pathname of
available, then it will return
Mark a time. the executable that started the
null.
console.time(label); process.
This method should only be called
Finish timer, record output. process.execPath;
in non-flowing mode. In flowing-
console.timeEnd(label); This is the set of node-specific
mode, this method is called
Print a stack trace to stderr of command line options from the
automatically until the internal
the current position. executable that started the
buffer is drained.
console.trace(label); process.
readable.read([size]);
process.execArgv;
Call this function to cause the
stream to return strings of the
specified encoding instead of
Buffer objects.
readable.setEncoding(encoding);
This method will cause the readable Set the method to GET and calls server.timeout;
stream to resume emitting data req.end() automatically.
events. http.get(options, [callback]); HTTP - Messages
readable.resume(); Returns a new web server object.
In case of server request, the HTTP
This method will cause a stream in The requestListener is a function
version sent by the client. In the
flowing-mode to stop emitting data which is automatically added to the
case of client response, the HTTP
events. 'request' event.
version of the connected-to server.
readable.pause(); server =
message.httpVersion;
This method pulls all the data out http.createServer([requestListener]
The request/response headers
of a readable stream, and writes it );
object.
to the supplied destination, Begin accepting connections on the
message.headers;
automatically managing the flow so specified port and hostname.
The request/response trailers
that the destination is not server.listen(port, [hostname],
object. Only populated after the
overwhelmed by a fast readable [backlog], [callback]);
'end' event.
stream. Start a UNIX socket server
message.trailers;
readable.pipe(destination, listening for connections on the
The request method as a string.
[options]); given path.
Read only. Example: 'GET',
This method will remove the hooks server.listen(path, [callback]);
'DELETE'.
set up for a previous pipe() call. The handle object can be set to
message.method;
If the destination is not either a server or socket
Request URL string. This contains
specified, then all pipes are (anything with an underlying
only the URL that is present in the
removed. _handle member), or a {fd: <n>}
actual HTTP request.
readable.unpipe([destination]); object.
message.url;
This is useful in certain cases server.listen(handle, [callback]);
The 3-digit HTTP response status
where a stream is being consumed by Stops the server from accepting new
code. E.G. 404.
a parser, which needs to connections.
message.statusCode;
"un-consume" some data that it has server.close([callback]);
The net.Socket object associated
optimistically pulled out of the Sets the timeout value for sockets,
with the connection.
source, so that the stream can be and emits a 'timeout' event on the
message.socket;
passed on to some other party. Server object, passing the socket
Calls
readable.unshift(chunk); as an argument, if a timeout
message.connection.setTimeout(msecs
occurs.
, callback).
HTTP server.setTimeout(msecs,
message.setTimeout(msecs,
callback);
A collection of all the standard callback);
Limits maximum incoming headers
HTTP response status codes, and the
count, equal to 1000 by default. If
short description of each.
set to 0 - no limit will be
http.STATUS_CODES;
applied.
This function allows one to
server.maxHeadersCount;
transparently issue requests.
The number of milliseconds of
http.request(options, [callback]);
inactivity before a socket is
presumed to have timed out.