9781449398583
index.html

Up and Running with Node.js

Tom Hughes-Croucher

2010


Author's Note
Preface
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
I. Core Concepts
1. Introduction
A very brief introduction to Node.js
2. Why Node
2.1 Professionalism in JavaScript
2.2 Browser Wars 2.0
3. Understanding Node.js
The Event Loop
II. Writing Code with Node.js
4. Getting Started
Installing Node.js
First Steps in Code
Node REPL
A First Server
5. Core APIs
Events
EventEmitter
Callback syntax
HTTP
HTTP Servers
HTTP Clients
URL
querystring
I/O
Streams
FileSystem
Buffers
Console.log
6. Helper APIs
Helpers
DNS
Crypto
Processes
Process
Child Process
III. API Documentation
7. API Documentation
TTY
tty.open(path, args=[])
tty.isatty(fd)
tty.setRawMode(mode)
tty.setWindowSize(fd, row, col)
tty.getWindowSize(fd)
Debugger
Advanced Usage
Assert
assert.fail(actual, expected, message, operator)
assert.ok(value, [message])
assert.equal(actual, expected, [message])
assert.notEqual(actual, expected, [message])
assert.deepEqual(actual, expected, [message])
assert.notDeepEqual(actual, expected, [message])
assert.strictEqual(actual, expected, [message])
assert.notStrictEqual(actual, expected, [message])
assert.throws(block, [error], [message])
assert.doesNotThrow(block, [error], [message])
assert.ifError(value)
Child Processes
Event: 'exit'
child.stdin
child.stdout
child.stderr
child.pid
child_process.spawn(command, args=[], [options])
child_process.exec(command, [options], callback)
child.kill(signal='SIGTERM')
REPL
repl.start(prompt='> ', stream=process.stdin)
REPL Features
Query String
querystring.stringify(obj, sep='&', eq='=')
querystring.parse(str, sep='&', eq='=')
querystring.escape
querystring.unescape
URL
url.parse(urlStr, parseQueryString=false)
url.format(urlObj)
url.resolve(from, to)
HTTPS
https.Server
https.createServer
https.request(options, callback)
https.get(options, callback)
UDP / Datagram Sockets
Event: 'message'
Event: 'listening'
Event: 'close'
dgram.createSocket(type, [callback])
dgram.send(buf, offset, length, path, [callback])
dgram.send(buf, offset, length, port, address, [callback])
dgram.bind(path)
dgram.bind(port, [address])
dgram.close()
dgram.address()
dgram.setBroadcast(flag)
dgram.setTTL(ttl)
dgram.setMulticastTTL(ttl)
dgram.setMulticastLoopback(flag)
dgram.addMembership(multicastAddress, [multicastInterface])
dgram.dropMembership(multicastAddress, [multicastInterface])
net
net.createServer(connectionListener)
net.createConnection(arguments...)
net.Server
net.Socket
net.isIP
Path
path.normalize(p)
path.join([path1], [path2], [...])
path.resolve([from ...], to)
path.dirname(p)
path.basename(p, [ext])
path.extname(p)
path.exists(p, [callback])
os Module
os.hostname()
os.type()
os.release()
os.uptime()
os.loadavg()
os.totalmem()
os.freemem()
os.cpus()
TLS (SSL)
s = tls.connect(port, [host], [options], callback)
tls.Server
Crypto
crypto.createCredentials(details)
crypto.createHash(algorithm)
hash.update(data)
hash.digest(encoding='binary')
crypto.createHmac(algorithm, key)
hmac.update(data)
hmac.digest(encoding='binary')
crypto.createCipher(algorithm, key)
cipher.update(data, input_encoding='binary', output_encoding='binary')
cipher.final(output_encoding='binary')
crypto.createDecipher(algorithm, key)
decipher.update(data, input_encoding='binary', output_encoding='binary')
decipher.final(output_encoding='binary')
crypto.createSign(algorithm)
signer.update(data)
signer.sign(private_key, output_format='binary')
crypto.createVerify(algorithm)
verifier.update(data)
verifier.verify(cert, signature, signature_format='binary')
Streams
Readable Stream
Event: 'data'
Event: 'end'
Event: 'error'
Event: 'close'
Event: 'fd'
stream.readable
stream.setEncoding(encoding)
stream.pause()
stream.resume()
stream.destroy()
stream.destroySoon()
stream.pipe(destination, [options])
Writable Stream
Event: 'drain'
Event: 'error'
Event: 'close'
Event: 'pipe'
stream.writable
stream.write(string, encoding='utf8', [fd])
stream.write(buffer)
stream.end()
stream.end(string, encoding)
stream.end(buffer)
stream.destroy()
Buffers
new Buffer(size)
new Buffer(array)
new Buffer(str, encoding='utf8')
buffer.write(string, offset=0, encoding='utf8')
buffer.toString(encoding, start=0, end=buffer.length)
buffer[index]
Buffer.isBuffer(obj)
Buffer.byteLength(string, encoding='utf8')
buffer.length
buffer.copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
buffer.slice(start, end=buffer.length)
Events
events.EventEmitter
Child Processes
Event: 'exit'
child.stdin
child.stdout
child.stderr
child.pid
child_process.spawn(command, args=[], [options])
child_process.exec(command, [options], callback)
child.kill(signal='SIGTERM')
Addons
Modules
Core Modules
File Modules
Loading from node_modules Folders
Folders as Modules
Caching
All Together...
Loading from the require.paths Folders
Addenda: Package Manager Tips
Timers
setTimeout(callback, delay, [arg], [...])
clearTimeout(timeoutId)
setInterval(callback, delay, [arg], [...])
clearInterval(intervalId)
Global Objects
global
process
require()
require.resolve()
require.paths
__filename
__dirname
module
Synopsis
DNS
dns.lookup(domain, family=null, callback)
dns.resolve(domain, rrtype='A', callback)
dns.resolve4(domain, callback)
dns.resolve6(domain, callback)
dns.resolveMx(domain, callback)
dns.resolveTxt(domain, callback)
dns.resolveSrv(domain, callback)
dns.reverse(ip, callback)
File System
fs.rename(path1, path2, [callback])
fs.renameSync(path1, path2)
fs.truncate(fd, len, [callback])
fs.truncateSync(fd, len)
fs.chmod(path, mode, [callback])
fs.chmodSync(path, mode)
fs.stat(path, [callback])
fs.lstat(path, [callback])
fs.fstat(fd, [callback])
fs.statSync(path)
fs.lstatSync(path)
fs.fstatSync(fd)
fs.link(srcpath, dstpath, [callback])
fs.linkSync(srcpath, dstpath)
fs.symlink(linkdata, path, [callback])
fs.symlinkSync(linkdata, path)
fs.readlink(path, [callback])
fs.readlinkSync(path)
fs.realpath(path, [callback])
fs.realpathSync(path)
fs.unlink(path, [callback])
fs.unlinkSync(path)
fs.rmdir(path, [callback])
fs.rmdirSync(path)
fs.mkdir(path, mode, [callback])
fs.mkdirSync(path, mode)
fs.readdir(path, [callback])
fs.readdirSync(path)
fs.close(fd, [callback])
fs.closeSync(fd)
fs.open(path, flags, [mode], [callback])
fs.openSync(path, flags, [mode])
fs.write(fd, buffer, offset, length, position, [callback])
fs.writeSync(fd, buffer, offset, length, position)
fs.writeSync(fd, str, position, encoding='utf8')
fs.read(fd, buffer, offset, length, position, [callback])
fs.readSync(fd, buffer, offset, length, position)
fs.readSync(fd, length, position, encoding)
fs.readFile(filename, [encoding], [callback])
fs.readFileSync(filename, [encoding])
fs.writeFile(filename, data, encoding='utf8', [callback])
fs.writeFileSync(filename, data, encoding='utf8')
fs.watchFile(filename, [options], listener)
fs.unwatchFile(filename)
fs.Stats
fs.ReadStream
fs.createReadStream(path, [options])
fs.WriteStream
Event: 'open'
fs.createWriteStream(path, [options])
util
util.debug(string)
util.log(string)
util.inspect(object, showHidden=false, depth=2)
util.pump(readableStream, writableStream, [callback])
util.inherits(constructor, superConstructor)
Executing JavaScript
vm.runInThisContext(code, [filename])
vm.runInNewContext(code, [sandbox], [filename])
vm.createScript(code, [filename])
script.runInThisContext()
script.runInNewContext([sandbox])
HTTP
http.Server
Event: 'request'
Event: 'connection'
Event: 'close'
Event: 'request'
Event: 'checkContinue'
Event: 'upgrade'
Event: 'clientError'
http.createServer(requestListener)
server.listen(port, [hostname], [callback])
server.listen(path, [callback])
server.close()
http.ServerRequest
Event: 'data'
Event: 'end'
request.method
request.url
request.headers
request.trailers
request.httpVersion
request.setEncoding(encoding=null)
request.pause()
request.resume()
request.connection
http.ServerResponse
response.writeContinue()
response.writeHead(statusCode, [reasonPhrase], [headers])
response.statusCode
response.setHeader(name, value)
response.getHeader(name)
response.removeHeader(name)
response.write(chunk, encoding='utf8')
response.addTrailers(headers)
response.end([data], [encoding])
http.request(options, callback)
http.get(options, callback)
http.Agent
http.getAgent(host, port)
Event: 'upgrade'
Event: 'continue'
agent.maxSockets
agent.sockets
agent.queue
http.ClientRequest
Event 'response'
request.write(chunk, encoding='utf8')
request.end([data], [encoding])
request.abort()
http.ClientResponse
Event: 'data'
Event: 'end'
response.statusCode
response.httpVersion
response.headers
response.trailers
response.setEncoding(encoding=null)
response.pause()
response.resume()
A. Installing Platform Dependencies
B. A Short Guide to JavaScript
C. WTFJS - Learning JavaScript's quirks
D. API documentation attribution
Node MIT License
Glossary
Site last updated on: September 15, 2011 at 11:20:17 AM PDT
Cover for Up and Running with Node.js